0

i want to echo only fromatted_address from json but dont know how?
code below:

    <?PHP
$ch = curl_init();
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=22.569794,88.357934';
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_HEADER, False);
curl_setopt($ch, CURLOPT_NOBODY, False);   //only header 
print "<pre>";
$bko = print_r(curl_exec($ch));
print "</pre>";
?>
Arun Pandey
  • 19
  • 1
  • 5

1 Answers1

0

You just need to pass the returned json string into json_decode to convert it to an array, then access the keys of the array as you normally would do.

$array = json_decode($json_result, true);

foreach($array['results'] as $k => $result) {
 echo $result['formatted_address'] . PHP_EOL;   
}

returns

50, Chittaranjan Ave, Bowbazar, Kolkata, West Bengal 700012, India

CR Ave Rd, Bowbazar, Kolkata, West Bengal 700012, India

Bowbazar, Kolkata, West Bengal, India

Calcutta, West Bengal 700001, India

Calcutta, West Bengal 700012, India

Calcutta, West Bengal, India

West Bengal, India

India

example

Community
  • 1
  • 1
DevDonkey
  • 4,835
  • 2
  • 27
  • 41