Decode sparse json array 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 $mynewarray10 will work fine.

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, $mynewarray10 will work fine.

Thanks Chris, this is exactly what I needed. – Isaac Sutherland Mar 21 '10 at 0:07.

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.

It has "10" index which can't be accessed through operator in PHP – vava Mar 20 '10 at 21:21 Ah, got it, it serialize the key as a string that happens to be a number, which you can't get at. The get_object_vars solution should solves that, as well as the second parameter of json_decode, mentioned above. – Alan Storm Mar 20 '10 at 23:45.

The problem is in the conversion from object to array. $a = (array)json_decode('{"10":"hi","test20":"howdy"}'); var_dump($a); //outputs array(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); } //outputs string(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); //outputs string(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($a10); //outputs string(2) "hi.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions