dsbifvcxr458755493 2016-12-07 03:56
浏览 23
已采纳

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

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?

  • 写回答

2条回答 默认 最新

  • douqiu0796 2016-12-07 05:26
    关注

    Simply you can try the below code...

    <?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条)

报告相同问题?

悬赏问题

  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的