|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-06-17 10:19 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 12:00:01 2025 UTC |
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.