|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52108 PHP-Bug in Array Key
Submitted: 2010-06-17 10:19 UTC Modified: 2010-06-20 01:07 UTC
From: goebel at arsmedia-software dot de Assigned:
Status: Not a bug Package: JSON related
PHP Version: Irrelevant OS: Ubuntu 9.04 (AMD64)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: goebel at arsmedia-software dot de
New email:
PHP Version: OS:


 [2010-06-17 10:19 UTC] goebel at arsmedia-software dot de

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:
"2" => stdClass object
public anzahl = "22"
public preis = "3"
public einheit = ""
public beschreibung = ""
public mwst = ""

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-19 12:26 UTC] 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:


//$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-20 01:07 UTC]
-Status: Open +Status: Bogus
 [2010-06-20 01:07 UTC]
See bug #45959
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 15 01:01:29 2024 UTC