Bug #70692 [NEW]: Undefined offset in array, but key is set

From: Date: Mon, 12 Oct 2015 06:59:05 +0000
Subject: Bug #70692 [NEW]: Undefined offset in array, but key is set
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-196540@lists.php.net to get a copy of this message
From: havenard at hotmail dot com Operating system: Windows PHP version: 5.6.14 Package: JSON related Bug Type: Bug Bug description:Undefined offset in array, but key is set Description: ------------ When converting a stdClass object instanced by json_decode() to array, the array is aparently fine with keys properly set as seen with print_r(), but they do not work. Trying to fetch an element will result in Notice: Undefined offset. Test script: --------------- <?php $data = [ 123 => (object)['name' => 'Example'] ]; print_r($data); // for reference, this shows the structure of the array $data echo $data[123]->name; // working as intented $data = json_encode($data); // serialize $data to {"123":{"name":"Example"}} echo "\n\n\n"; $data = json_decode($data); // unserialize json $data = (array)$data; // even though it was originally an array, $data is an object because JavaScript does not support non-contiguous arrays, so we have to explicitly convert to array print_r($data); // this shows the structure of the array $data looks just like it was before, all should be fine, but then... echo $data[123]->name; // BOOM! Notice: Undefined offset: 123 Expected result: ---------------- Array ( [123] => stdClass Object ( [name] => Example ) ) Example Array ( [123] => stdClass Object ( [name] => Example ) ) Example Actual result: -------------- Array ( [123] => stdClass Object ( [name] => Example ) ) Example Array ( [123] => stdClass Object ( [name] => Example ) ) PHP Notice: Undefined offset: 123 in test_script.php on line 19 -- Edit bug report at https://bugs.php.net/bug.php?id=70692&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70692&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70692&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70692&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=70692&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=70692&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=70692&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=70692&r=needscript Try newer version: https://bugs.php.net/fix.php?id=70692&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=70692&r=support Expected behavior: https://bugs.php.net/fix.php?id=70692&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=70692&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=70692&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=70692&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70692&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=70692&r=dst IIS Stability: https://bugs.php.net/fix.php?id=70692&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=70692&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=70692&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=70692&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=70692&r=mysqlcfg

« previous php.bugs (#196540) next »