나는 PHP 함수에서 다음을 가지고있다;PHP 함수에서 생성 된 배열 값을 검색하려면 어떻게해야합니까?
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
)
위도와 경도가 포함 된 함수 외부에서 변수를 만드는 방법을 알지 못합니다. 이것 같이;
$lat = $koords[0]; // does NOT work
$lat = $array[0]; // does NOT work
내가 잊어 버린 것은 무엇입니까? 배열의 두 요소 각각에 다른 PHP 변수를 설정하려면 어떻게해야합니까?
하셨습니까 그것을 얻지 못한다. 당신이 할 수 없다는 뜻입니까? '$ koords = geocode ('some address'); $ lat = $ koords [0]? ' – Peter
'$ 결과 = geocode ('some address '); $ lat = $ results [0];' – lsouza
내가 $ lat = $ koords [0]이라고 말하면; 나는 아무것도 돌려받지 못한다. 작동하지 않습니다. 왜? –