Decode sparse json object to php array Decode sparse json object to php array json json

Decode sparse json object to php array


json_decode returns an object of type stdClass by default. You access members as properties (i.e., $result->test20). 10 isn't a valid name for a property, which is why you're losing it.

Instead of casting to an array, you can pass true as a second argument to json_decode to make it return an associative array itself:

$mynewarray = json_decode($json, true);

If you do that, $mynewarray[10] will work fine.


What version of PHP? On 5.2 the following program/script

$myarray = array(10=>'hi','test20'=>'howdy');$json = json_encode($myarray);$mynewarray = (array) json_decode($json);var_dump($mynewarray);

Outputs

array(2) {  ["10"]=>  string(2) "hi"  ["test20"]=>  string(5) "howdy"}

Which doesn't display the behavior you're describing.

That said, if your version of PHP is miscasting the JSON, try using get_object_vars on the stdClass object that json_decode returns

get_object_vars(json_decode($json))

That might return better results.


The problem is in the conversion from object to array.

$a = (array)json_decode('{"10":"hi","test20":"howdy"}');var_dump($a);//outputsarray(2) {  ["10"]=>     string(2) "hi"  ["test20"]=>     string(5) "howdy"}

See how this array have index "10"? But in PHP, everything that looks like a number gets converted into a number, especially in array indexes. You can't just get a["10"] because it converts "10" into a number and this array does not have such an index.

However, foreach works.

foreach ($a as $key => $value) {   var_dump($key);   var_dump($value);}//outputsstring(2) "10"string(2) "hi"string(6) "test20"string(5) "howdy"

You can also treat result of json_decode as an object. While you won't be able to do $a->10 or $a->"10",

$a = json_decode('{"10":"hi","test20":"howdy"}');$b = 10;var_dump($a->$b);//outputsstring(2) "hi"

works.

But most likely, as Chris said, you just want to pass true as a second argument.

$a = json_decode('{"10":"hi","test20":"howdy"}', true);var_dump($a[10]);//outputsstring(2) "hi"