|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-12-05 10:40 UTC] markus dot kristo at esportnetwork dot com
Description:
------------
json_encoding and json_decoding an array with string indices as numbers starting from anything else than zero will create an array that can't be used to fetch elements from.
Note that this only happens when you cast the resulting object from json_decode to an array.
Please look at the attached code example for more information about this bug.
Reproduce code:
---------------
<?php
$array = array("1" => "one"); // array("0" => "zero") works fine.
$foo = (array) json_decode(json_encode($array));
// $foo = json_decode(json_encode($array), true) works fine
var_dump($foo);
// array(1) {
// ["1"]=>
// string(3) "one"
// }
var_dump($foo["1"]); // Outputs PHP Notice and NULL expected "one"
?>
Expected result:
----------------
I expect the last var_dump to output "one"
Actual result:
--------------
The last var_dump outputs NULL and a PHP Notice "undefined index: 1" is shown
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 13:00:01 2025 UTC |
This has nothing to do with JSON at all, same happens with this: <?php $a->{"1"} = "one"; $foo = (array) $a; var_dump($foo); var_dump($foo["1"]); ?>