title: Get location from latitude and longitude in PHP
body: <p>I want to get <strong>address, city, state</strong> and <strong>country</strong> in different variables so that I can display it separately but due to different latlong, somewhere I am getting whole address and somewhere only state and country, so I am not able to get a specific address due to changing latlong.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>$geoLocation=array();
  $URL = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=8.407168,6.152344&amp;sensor=false';
  $data = file_get_contents($URL);
  $geoAry = json_decode($data,true);
  for($i=0;$i&lt;count($geoAry['results']);$i++){
     if($geoAry['results'][$i]['types'][0]=='sublocality_level_1'){
       $address=$geoAry['results'][$i]['address_components'][0]['long_name'];
       $city=$geoAry['results'][$i]['address_components'][1]['long_name'];
       $state=$geoAry['results'][$i]['address_components'][3]['long_name'];
       $country=$geoAry['results'][$i]['address_components'][4]['long_name'];
       break;
    }
    else {
       $address=$geoAry['results'][0]['address_components'][2]['long_name'];
       $city=$geoAry['results'][0]['address_components'][3]['long_name'];
       $state=$geoAry['results'][0]['address_components'][5]['long_name'];
       $country=$geoAry['results'][0]['address_components'][6]['long_name'];
    }
  }
  $geoLocation = array(
    'city'=&gt;$city,
    'state'=&gt;$state,
    'country'=&gt;$country,
    'address'=&gt;$address
  );
  print_r($geoLocation);
  </code></pre>