2016-12-07 1 views
2

나는 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 변수를 설정하려면 어떻게해야합니까?

+0

하셨습니까 그것을 얻지 못한다. 당신이 할 수 없다는 뜻입니까? '$ koords = geocode ('some address'); $ lat = $ koords [0]? ' – Peter

+0

'$ 결과 = geocode ('some address '); $ lat = $ results [0];' – lsouza

+0

내가 $ lat = $ koords [0]이라고 말하면; 나는 아무것도 돌려받지 못한다. 작동하지 않습니다. 왜? –

답변

1

간단히 아래 코드를 시도 할 수 있습니다 ... $results 이후

<?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 = geocode('some address'); 
//  ^You have to remove print_r() from here otherwise all things are alright 
print_r($results); 

// You may need to use $results[0], $results[1], etc respectively to getting result 
echo $results[0]; 
echo $results[1]; 
echo $results[2]; 
?> 
0

일부 데이터 (Array-Type Data가), 당신은 효과적으로 대신 같은 것을 할 수 포함

<?php 

    // ASSIGN THE DATA RETURNED BY CALLING THE `geocode()` FUNCTION 
    // TO A VARIABLE (HERE: $results) 
    $results = geocode('some address'); //<== NOTICE: "NO print_r() HERE!" 


    // CREATE 2 VARIABLES TO HOLD YOUR LATITUDE & LONGITUDE 
    // INITIALIZE THESE VARIABLES TO NULL 
    $lat  = null; 
    $long  = null; 


    // TO EXTRACT THE LATITUDE & LONGITUDE VALUES 
    // STORED IN THE NUMERICALLY INDEXED ARRAY: $results, 
    // 1ST: CHECK IF $results CONTAINS SOME DATA: 

    if(!empty($results)){ 
     // IF IT DOES; ASSIGN VALUES TO $lat & $long RESPECTIVELY. 
     $lat = $results[0]; //<== ASSUMES THE LATITUDE VALUE IS STORED @INDEX 0 
     $long = $results[1]; //<== ASSUMES THE LONGITUDE VALUE IS STORED @INDEX 1 
    } 

    // NOW TRY TO SEE THE RESULT OF THE ACTIONS ABOVE: 
    var_dump($results); 
    var_dump($lat); 
    var_dump($long); 
관련 문제