Bug #53130 [Bgs]: JSON decoded arrays have broken integer keys
| From: | manchokapitancho at gmail dot com | Date: | Fri, 22 Oct 2010 07:14:35 +0000 |
| Subject: | Bug #53130 [Bgs]: JSON decoded arrays have broken integer keys | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-155437@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=53130&edit=1
ID: 53130
User updated by: manchokapitancho at gmail dot com
Reported by: manchokapitancho at gmail dot com
Summary: JSON decoded arrays have broken integer keys
Status: Bogus
Type: Bug
Package: JSON related
PHP Version: 5.3.3
Block user comment: N
New Comment:
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!
Previous Comments:
------------------------------------------------------------------------
[2010-10-22 09:01:59] 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-21 14:50:03] 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)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=53130&edit=1