Reputation: 169
I want to echo value in the json array one by one when i want
<?php
function get_web_page($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false // don't return headers
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
$response = get_web_page("http://maps.googleapis.com/maps/api/geocode/json?address=colombo");
$resArr = array();
$resArr = json_decode($response);
echo "<pre>";
print_r($resArr);
echo "</pre>";
echo "<br>";
echo "<br>";
;
?>
This is the current result in the browser above code
stdClass Object
(
[results] => Array
(
[0] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Colombo
[short_name] => Colombo
[types] => Array
(
[0] => locality
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Colombo
[short_name] => Colombo
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Western Province
[short_name] => WP
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => Sri Lanka
[short_name] => LK
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Colombo, Sri Lanka
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 6.9805844
[lng] => 79.8900852
)
[southwest] => stdClass Object
(
[lat] => 6.8625113
[lng] => 79.8225192
)
)
[location] => stdClass Object
(
[lat] => 6.9270786
[lng] => 79.861243
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 6.9805844
[lng] => 79.8900852
)
[southwest] => stdClass Object
(
[lat] => 6.8625113
[lng] => 79.8225192
)
)
)
[place_id] => ChIJA3B6D9FT4joRjYPTMk0uCzI
[types] => Array
(
[0] => locality
[1] => political
)
)
)
[status] => OK
)
I want to echo value by value
Upvotes: 2
Views: 4285
Reputation: 7896
try it with:
$resArr = json_decode($response, true);
It will convert it into associative array format.
For more detail have a look at JSON Decode PHP
Upvotes: 2