Php – json_decode to array

arraysjsonPHP

I am trying to decode a JSON string into an array but i get the following error.

Fatal error: Cannot use object of type
stdClass as array in
C:\wamp\www\temp\asklaila.php on line
6

Here is the code:

<?php
$json_string = 'http://www.domain.com/jsondata.json';

$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata);
print_r($obj['Result']);
?>

Best Answer

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);