json_decode to array
As per the documentation, you need to specify true
as the second argument if you want an associative array instead of an object from json_decode
. This would be the code:
$result = json_decode($jsondata, true);
If you want integer
keys instead of whatever the property names are:
$result = array_values(json_decode($jsondata, true));
However, with your current decode you just access it as an object:
print_r($obj->Result);
This is a late contribution, but there is a valid case for casting json_decode
with (array)
.
Consider the following:
$jsondata = '';$arr = json_decode($jsondata, true);foreach ($arr as $k=>$v){ echo $v; // etc.}
If $jsondata
is ever returned as an empty string (as in my experience it often is), json_decode
will return NULL
, resulting in the error Warning: Invalid argument supplied for foreach() on line 3. You could add a line of if/then code or a ternary operator, but IMO it's cleaner to simply change line 2 to ...
$arr = (array) json_decode($jsondata,true);
... unless you are json_decode
ing millions of large arrays at once, in which case as @TCB13 points out, performance could be negatively effected.