'How can I access the value of JSON which has Array in Object?
I wish to know an simple way to access the value inside following JSON, which owns an Array. I am codeing in PHP:
<?php
$json = '{"flight": "7800", "city": "New York", "time":[{"Houston":"17:34","Los Angles":"21:23"}]}';
$str = json_decode($json);
echo $str->city; // New York
//echo $str->time->Houston; // How can I access the valus of Houston: 17:34
?>
Solution 1:[1]
Decode the Json-String as assoc array:
$json = '{"flight": "7800", "city": "New York", "time":[{"Houston":"17:34","Los Angeles":"21:23"}]}';
$flight = json_decode($json, true);// You get associative array in this case
echo "<br>" . $flight['city']; // New York
echo "<br>" . $flight['time'][0]['Houston'];
echo "<br>" . $flight['time'][0]['Los Angeles'];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | John |