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);
Answer from Stephen on Stack OverflowAs 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);
try this
$json_string = 'http://www.domain.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
echo "<pre>";
print_r($obj);
Videos
$string = file_get_contents('./string.json');
$json = json_decode($string);
if you want to have items: <address>:
foreach ($json['items'] as $address)
{
echo "items:". $address['address'] ."\n";
};
anyway if you are not sure about how an array is built you could print it via:
print_r($json);
which will print:
Array
(
[items] => Array
(
[0] => Array
(
[address] => W 7th Ave
)
[1] => Array
(
[address] => W 8th St
)
)
)
now you found out that $json contains just an array (items) of two array, then, if you loop it, you will get that array which is printed in your example.
As explained above you need to go one step deeper by looping the elements in your items array and print their address element.
here is the complete script: http://pastie.org/2275879
Your items are in an array. You could loop through them like this:
foreach ($json['items'] as $address)
{
echo 'Address: '.$address;
}
json_decode() does so work. The second param turns the result in to an array:
var_dump(json_decode('[{"a":"b"},{"c":"d"},{"e":"f"}]', true));
// gives
array(3) {
[0]=>
array(1) {
["a"]=>
string(1) "b"
}
[1]=>
array(1) {
["c"]=>
string(1) "d"
}
[2]=>
array(1) {
["e"]=>
string(1) "f"
}
}
$array = '[{"a":"b"},{"c":"d"},{"e":"f"}]';
print_r(json_decode($array, true));
Read the manual - parameters for the json_decode method are clearly defined:
http://www.php.net/manual/en/function.json-decode.php