dave_biscuits Posted November 7, 2006 Share Posted November 7, 2006 Hi Everyone,Got a problem with string offsets ... ie:[quote]2. Illegal use of string offsets causes E_ERROR instead of E_WARNING. EX : <?php $a = "foo"; unset($a[0][1][2]); ?> Fatal error: Cannot use string offset as an array in ... on line 1[/quote]my specific code is fowling on:[code]$data2[$blah]['status'] = $results[$datastream['type']][$datastream['number']]['status'];[/code]because $results[$datastream['type']][$datastream['number']]['status'] doesnt exist, it throws up a string offset error (which it never used to do in php4... php4 just treated it as null ... )anyone got any tips / suggestions how i can get around this problem in php5? I'd like to start using php5 (mainly to learn pdflib but thats another story) but not if it means i have to rewrite copious amounts of php4 code ... i liked my php4 multidimensional associative arrays as they were before ;DThanks in advance!Dave. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 7, 2006 Share Posted November 7, 2006 [quote]because $results[$datastream['type']][$datastream['number']]['status'] doesnt exist, ...[/quote]It's not because it doesn't exist.. it's because it's a string rather than an array.You can do an is_array() beforehand to see if it's an array. Or you can stop putting strings where your code later expects to find an array :) Quote Link to comment Share on other sites More sharing options...
dave_biscuits Posted November 7, 2006 Author Share Posted November 7, 2006 is_array() sounds like a winner ... except:[code]if(is_array($barrow[$jobinfobase2['uid']]['uid'])) { echo "its an array"; } else { echo "its not an array"; }[/code]gives the error:[code]PHP Fatal error: Cannot use string offset as an array in /usr/local/www/data-dist/Manhattan/g2a_price.php on line 811[/code]I havent even set $barrow[$jobinfobase2['uid']]['uid'] so i have no idea why it automatically assumes its a string offset ...???Cheers,Dave. Quote Link to comment Share on other sites More sharing options...
heckenschutze Posted November 7, 2006 Share Posted November 7, 2006 Unfortunatly PHP has it's downside, Unlike C where you could refer to the element in the 'string', by an index... since a string is really an array of char's.You could also convert the string to an array... Quote Link to comment Share on other sites More sharing options...
btherl Posted November 7, 2006 Share Posted November 7, 2006 What's going on there is that the array is further up.. for the expression$barrow[$jobinfobase2['uid']]['uid']to work, $barrow must be an array, $jobinfobase2['uid'] must be an array, AND $barrow[$jobinfobase2['uid']] must be an array. If any one of them is not an array, then the expression doesn't make sense. How can you find the 'uid' element of a string?It's likely there's a bug in your code causing something to be set to a string when you want it to be an array instead. It may be at ANY level of that multi-level array you are working with. Try doing a var_dump($barrow) just before the error and see what output you get. Quote Link to comment Share on other sites More sharing options...
trq Posted November 7, 2006 Share Posted November 7, 2006 String offsets use {} in php5. This is the second time this has been changed, seems the devs cannot decide which way to go.[code]<?php $a = "foobar"; echo $a{3} // produces b?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.