#46758 [Opn->Bgs]: casting object to array messes numeric indexes

From: Date: Tue, 30 Dec 2008 20:39:18 +0000
Subject: #46758 [Opn->Bgs]: casting object to array messes numeric indexes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-132423@lists.php.net to get a copy of this message
ID: 46758 Updated by: felipe@php.net Reported By: markus dot kristo at esportnetwork dot com -Status: Open +Status: Bogus Bug Type: Scripting Engine problem Operating System: * PHP Version: 5.2.7 New Comment: That is an known issue, it's documented: http://docs.php.net/manual/en/language.types.array.php Thanks. Previous Comments: ------------------------------------------------------------------------ [2008-12-08 11:28:36] jani@php.net 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"]); ?> ------------------------------------------------------------------------ [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

« previous php.bugs (#132423) next »