|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-07-17 10:12 UTC] php at kingsquare dot nl
Description:
------------
Creating objects from (i.e.) a JSON string and casting that to an array results in something that looks like an array, acts like an array, but in reality is still an object? (or atleast suffers from not supporting numeric properties on objects)
I know objects can't have numeric properties but dumping the object _and_ array works as intended... then accessing the array by index 0 it fails? (but accessing the object property like $obj->{"0"} works, thus i'd expect $array[0] / $array['0'] to work as well ?
(see reports: 45346 / 61655 perhaps?)
I know about the lookup / juggling mismatch (the index is actually a string "0" but access via index is juggled into an int and thus cant be found...) Although i understand the problem it has some issues:
first and foremost: i expected it to work ;) (since creating an $foo = array("1"=>"test") will work with $foo[1], so from a user perspective there should be no difference
- this specific use case seems undocumented ?
- If var_dump-ing the resulting array i should be able to either use array_key_exists or by derefencing to access the key ... If not: var_dump should not show it / mention it.. ;)
Test script:
---------------
$json = '{ "0" : "test1" }';
// just decode that into an object
$obj = json_decode($json);
// recast it into an array
$array = (array) $obj;
// just dump to see the results
var_dump($obj, $array, $array['0'], $array[0]);
Actual result:
--------------
Notices on the undefined index: 0
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 10 13:00:02 2025 UTC |
In this case i'd expect $array to look like array(1) { [0]=> string(5) "test1" } instead of array(1) { ["0"]=> string(5) "test1" } test code: <?php $json = '{ "0" : "test1" }'; // just decode that into an object $obj = json_decode($json); // recast it into an array $array = get_object_vars($obj); // just dump to see the results var_dump($obj,$array, $array[0], $array['0']); //object(stdClass)#1 (1) { ["0"]=> string(5) "test1" } array(1) { [0]=> string(5) "test1" } string(5) "test1" string(5) "test1" $array2 = (array) $obj; var_dump($obj,$array2, $array2[0], $array2['0']); //E_NOTICE : type 8 -- Undefined offset: 0 -- at line 10 E_NOTICE : type 8 -- Undefined index: 0 -- at line 10 $array3 = json_decode($json,true); var_dump($array3, $array3[0], $array3['0']); //object(stdClass)#1 (1) { ["0"]=> string(5) "test1" } array(1) { ["0"]=> string(5) "test1" } NULL NULL array(1) { [0]=> string(5) "test1" } string(5) "test1" string(5) "test1" ?>I'd expect it to be a string due to the json data itself: $json = '{ "0" => "test" }'; and not: $json = '{ 0 => "test" }'; In either case: fetching the array using the index would juggle it into int anyway (thus $array[0] == $array["0"] == "test")True, and the point isn't that the key should be a string or an integer but that it isn't found at all :) $json = '{ "0" : "test1" }'; $obj = json_decode($json); // recast it into an array $array = (array) $obj; $array[0] == "test1" // throws error that the index can't be found but is shown in a var_dump