Bug #46758 [Com]: casting object to array messes numeric indexes

From: Date: Thu, 02 Mar 2017 07:48:45 +0000
Subject: Bug #46758 [Com]: casting object to array messes numeric indexes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207653@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=46758&edit=1 ID: 46758 Comment by: cweiske@php.net Reported by: markus dot kristo at esportnetwork dot com Summary: casting object to array messes numeric indexes Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: * PHP Version: 5.2.7 Block user comment: N Private report: N New Comment: The relevant section in the documentation: If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible [...] Previous Comments: ------------------------------------------------------------------------ [2008-12-30 20:39:17] felipe@php.net That is an known issue, it's documented: http://docs.php.net/manual/en/language.types.array.php Thanks. ------------------------------------------------------------------------ [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 https://bugs.php.net/bug.php?id=46758&edit=1

« previous php.bugs (#207653) next »