ID:               46758
 Updated by:       [EMAIL PROTECTED]
-Summary:          jsondecode
 Reported By:      markus dot kristo at esportnetwork dot com
 Status:           Open
-Bug Type:         Arrays related
+Bug Type:         Scripting Engine problem
-Operating System: Windows XP, Linux
+Operating System: *
 PHP Version:      5.2.7
 New Comment:

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"]);

?>


Previous Comments:
------------------------------------------------------------------------

[2008-12-05 10:40:45] 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


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=46758&edit=1

Reply via email to