Edit report at http://bugs.php.net/bug.php?id=52108&edit=1
ID: 52108 Updated by: fel...@php.net Reported by: goebel at arsmedia-software dot de Summary: PHP-Bug in Array Key -Status: Open +Status: Bogus Type: Bug Package: JSON related Operating System: Ubuntu 9.04 (AMD64) PHP Version: Irrelevant New Comment: See bug #45959 Previous Comments: ------------------------------------------------------------------------ [2010-06-19 12:26:41] anon at anon dot com Hi, just passing by. I don't know why it does that but here's a more complete test that does not require JSON: ------------ error_reporting(-1); //$obj = json_decode('{"2":123}'); $obj = new stdClass(); $obj->{"2"} = 123; var_dump($obj->{2}, $obj->{"2"}, $obj); $array = (array)$obj; var_dump($array[2], $array["2"], $array); ------------ It can access the int(123) via both 2 and "2" on the object, but even though the array var_dump shows the "2" key to be present, and it can be iterated, both [2] and ["2"] complain the offset/index is undefined. ------------------------------------------------------------------------ [2010-06-17 10:19:21] goebel at arsmedia-software dot de Description: ------------ Hello, I've found an error in the keys of an array. If I have a JSON-String and decode it with json_decode and cast the returned value in an array then I get NULL trying to access an array entry (eg array[1]). The return value should however be a stdClass object. Yours sincerely, Arne Göbel Test script: --------------- $jstr = '{"1": {"anzahl": "", "preis": "", "einheit": "", "beschreibung": "", "mwst": ""}, "2": {"anzahl": "22", "preis": "3", "einheit": "", "beschreibung": "", "mwst": ""}, "3": {"anzahl": "12", "preis": "22", "einheit": "", "beschreibung": "", "mwst": ""}, "4": {"anzahl": "", "preis": "", "einheit": "", "beschreibung": "", "mwst": ""}}'; $array = (array) json_decode($jstr); echo '<pre>'; var_dump($array[2], $array); echo phpversion(); Expected result: ---------------- array( "2" => stdClass object public anzahl = "22" public preis = "3" public einheit = "" public beschreibung = "" public mwst = "" ) Actual result: -------------- NULL ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=52108&edit=1