Is this an error accessing a PHP array?

I ran into this error where the element of the array, if its index is the string "0", is not available.

This is not an error with unserialize, as it happened in my code without calling it.

$arr = unserialize('a:1:{s:1:"0";i:5;}');
var_dump($arr["0"]); //should be 5, but is NULL
var_dump($arr[0]);   //maybe this would work?  no. NULL

Am I doing something wrong here? How to access this array element?

+3
source share
3 answers

Yes, it seems like this is a bug related to automatically converting PHP strings to integers. More information is available here: http://bugs.php.net/bug.php?id=43614

var_dump( $arr ); // => array(1) { ["0"]=>  int(5) } 
$arr2["0"]=5;
var_dump($arr2); // => array(1) { [0]=>  int(5) } 
print serialize($arr2); // a:1:{i:0;i:5;}

So, it seems that older versions of PHP5 do not perform a row index to convert an integer index to unserialize.

PHP 5.2.5 ​​ PHP 5.2.6 (. http://www.php.net/ChangeLog-5.php#5.2.6).

+6

var_dump , , . , . Perl, , Data:: Dumper

+1

Actually, the code in your question gives

int(5)
0
source

All Articles