Jump to content

string offsets - php 4 to php 5


dave_biscuits

Recommended Posts

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  ;D

Thanks in advance!
Dave.
Link to comment
https://forums.phpfreaks.com/topic/26405-string-offsets-php-4-to-php-5/
Share on other sites

[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 :)
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.
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.