如何检索在PHP函数中创建的数组值?

时间:2016-12-07 03:56:27

标签: 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变量?

2 个答案:

答案 0 :(得分:1)

您只需尝试以下代码......

<?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];
?>

答案 1 :(得分:0)

  

由于$results包含一些数据(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);