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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,