|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63007 json functions corrupt arrayindizes
Submitted: 2012-09-04 07:38 UTC Modified: 2012-09-04 14:37 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: janfili+phpbugs at gmail dot com Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.3.16 OS: Linux ubuntu1010
Private report: No CVE-ID: None
 [2012-09-04 07:38 UTC] janfili+phpbugs at gmail dot com
after json en/decoding the original array index cant be used to retrieve the values.

however foreach and all other array-related functions work as expected.

Test script:

$class = new stdClass();
$class->field = 'foo';
$array = array(1 => $class);
$json_sting = json_encode($array);
$array_obj = json_decode($json_sting);
$array = (array) $array_obj;


Expected result:
output of last var_dump is expected to be 

object(stdClass)#3 (1) {
    string(3) "foo"

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-04 08:04 UTC] reeze dot xia at gmail dot com
This is not a problem of JSON itself.

but. array vs object conversion. and it was documented:

after json_encode  key: 1 was encoded as string.
In object properties and only been string key.

but after convert to array, it will not be able to accessed since.
if the key will be changed to number key if possible.

similar to
 [2012-09-04 08:18 UTC] janfili+phpbugs at gmail dot com
using this var_dump($array['1']); as the last statement will not help either
 [2012-09-04 08:19 UTC] reeze dot xia at gmail dot com
Ah, I'm sorry, the bug ID: #54082, I can't find the report right now.

will post that later
 [2012-09-04 08:21 UTC] reeze dot xia at gmail dot com
Yes, it will not accessible anymore if you have property with string number.
since array handle keys different from objects.
 [2012-09-04 11:17 UTC]
-Status: Open +Status: Not a bug
 [2012-09-04 11:39 UTC] janfili+phpbugs at gmail dot com
not similar to

51915 mentions that the indexes become strings, and that is the case of course through json en/decoding.

The bug described in this ticket is that the elements belonging to these keys are not retrievable anymore. 

even if you explicitly get the keys

$k = array_keys($array);
echo $array[$k[0]];

will result in undefined index. Reductio ad absurdum
 [2012-09-04 13:17 UTC]
they are the same....

the array's key 1 has become to string "1"..
 [2012-09-04 14:16 UTC] janfili+phpbugs at gmail dot com
Would you please read my comments carefully again?

as i mentioned in earlier comments

it is NOT possible to get the values using

1. var_dump($array[1]);


2. var_dump($array['1']);


3. var_dump($array["1"]);


4. $k = array_keys($array);

All of these options will result into undefined index.

Even though the keys we convertet to strings, you cant get the values by using the string key. You cant even get the value if you get the keys by array_keys() first. Before posting something about string keys vs int keys now, just take a close look at 4.

best regards
 [2012-09-04 14:37 UTC]
would you read my comment clearly(or the comment of in #51951)?

as I said, the key has become to a string...

I knew what you are saying, but as I said, it's a knew behavior(or knew issue)...
 [2012-09-04 14:38 UTC] reeze dot xia at gmail dot com
Hi janfili, 
   It IS a bug, @see
object -> array have problem, so does array -> object have the same problem.

It is inconsistent, but it was documented.
 [2012-09-04 16:21 UTC] janfili+phpbugs at gmail dot com
This is a real duplicate:

And it ended in fixing the Documentation.
What is fine. But the Documentation did
 not get Update everywhere.

This page covers it just fine.
But this one doesnt

I see the language parameter in the url, but it in english all the time.
Can someone make all the pages up to date.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 20:01:31 2024 UTC