I have the following in PHP function;
function geocode($address){
$address = urlencode($address);
$url = "http://maps.google.com/maps/api/geocode/json?address={$address}";
$resp_json = file_get_contents($url);
$resp = json_decode($resp_json, true);
if($resp['status']=='OK'){
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
$formatted_address = $resp['results'][0]['formatted_address'];
if($lati && $longi && $formatted_address){
// put the data in the array
$koords = array();
array_push(
$koords,
$lati,
$longi,
$formatted_address
);
//print_r($koords); //Works fine here
return $koords;
}else{
return false;
}
}else{
return false;
}
}
$results = print_r(geocode('some address'); // shows me the output below
Array
(
[0] => 39.0098762
[1] => -94.4912607
)
But I don't know how to create a variable outside the function that contains the latitude and longitude. Like this;
$lat = $koords[0]; // does NOT work
$lat = $array[0]; // does NOT work
What (really simple) thing am I forgetting here? How do I set another PHP variable to each of the two elements of the array?