php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53130 JSON decoded arrays have broken integer keys
Submitted: 2010-10-21 14:50 UTC Modified: 2010-10-22 09:42 UTC
From: manchokapitancho at gmail dot com Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.3.3 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: manchokapitancho at gmail dot com
New email:
PHP Version: OS:

 

 [2010-10-21 14:50 UTC] manchokapitancho at gmail dot com
Description:
------------
JSON decoded arrays have their integer keys broken. They are present in the array 
but they can be neither accessed nor found through array_key_exists.

Test script:
---------------
$obj = json_decode ('{ "3" : "4", "text" : "5" }');
var_dump ($obj);
$arr = (array)$obj;
var_dump ($arr);
var_dump ($arr[3]);
var_dump ($arr["text"]);
var_dump (array_key_exists (3, $arr));
var_dump (array_key_exists ("text", $arr));


Expected result:
----------------
object(stdClass)#10 (2) { ["3"]=> string(1) "4" ["text"]=> string(1) "5" }
array(2) { ["3"]=> string(1) "4" ["text"]=> string(1) "5" }
string(1) "4"
string(1) "5"
bool(true)
bool(true)


Actual result:
--------------
object(stdClass)#10 (2) { ["3"]=> string(1) "4" ["text"]=> string(1) "5" }
array(2) { ["3"]=> string(1) "4" ["text"]=> string(1) "5" }
NULL
string(1) "5"
bool(false)
bool(true)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-22 09:01 UTC] aharvey@php.net
-Status: Open +Status: Bogus -Package: Arrays related +Package: JSON related
 [2010-10-22 09:01 UTC] aharvey@php.net
Besides being invalid JSON, that's an object rather than an array. Object property names generally follow the same rules as variable names, but can be accessed via braced literals like so:

<?php
$obj = json_decode ('{ "3" : "4", "text" : "5" }');
var_dump($obj->{3});       // outputs string(1) "4"
var_dump($obj->{'3'});     // outputs string(1) "4"
?>

Not a bug; closing.
 [2010-10-22 09:14 UTC] manchokapitancho at gmail dot com
A.Harvey, it seems that you haven't read the bug carefully enough and set it to 
bogus incorrectly. Please check this code:

$old_array = array (3 => 4, 'test' => 5);
$obj = json_decode (json_encode ($old_array));
$new_array = (array)$obj;
var_dump ($old_array); //outputs:array(2) { [3]=> int(4) ["test"]=> int(5) }
var_dump ($new_array); //outputs:array(2) { [3]=> int(4) ["test"]=> int(5) }
var_dump (array_key_exists (3, $old_array)); //outputs:bool(true)
var_dump (array_key_exists (3, $new_array)); //outputs:bool(false) !!!
var_dump ($old_array[3]); //outputs:int(4)
var_dump ($new_array[3]); //outputs:NULL !!!

The bug is obvious!
 [2010-10-22 09:20 UTC] manchokapitancho at gmail dot com
Additionaly, it's not about whether the numeric keys of an object could be access 
but about that json decoded object converted to array appears (var_dump) to have 
the numeric keys but they are not accessible. That is an incostistency no matter 
what the usages of this code are.
 [2010-10-22 09:20 UTC] aharvey@php.net
You're right, I missed the cast. My apologies.

Having said that, casting an object with integer keys to an array
results in the integer keys being inaccessible -- this is documented
in the manual at
http://php.net/language.types.array#language.types.array.casting

Access the object as an object and you won't have a problem.
 [2010-10-22 09:42 UTC] manchokapitancho at gmail dot com
Thank you! That explains the things. Have a nice day :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 06 20:01:27 2024 UTC