永远不必这样做,并且对格式以及如何从中访问特定变量感到困惑。

的结果 :

$result = json_decode($result);
print_r($result);

是 :
stdClass Object
(
    [input] => stdClass Object
        (
            [address_components] => stdClass Object
                (
                    [number] => 1109
                    [predirectional] => N
                    [street] => Highland
                    [suffix] => St
                    [formatted_street] => N Highland St
                    [city] => Arlington
                    [state] => VA
                    [zip] => 22201
                    [country] => US
                )

            [formatted_address] => 1109 N Highland St, Arlington, VA 22201
        )

    [results] => Array
        (
            [0] => stdClass Object
                (
                    [address_components] => stdClass Object
                        (
                            [number] => 1109
                            [predirectional] => N
                            [street] => Highland
                            [suffix] => St
                            [formatted_street] => N Highland St
                            [city] => Arlington
                            [county] => Arlington County
                            [state] => VA
                            [zip] => 22201
                            [country] => US
                        )

                    [formatted_address] => 1109 N Highland St, Arlington, VA 22201
                    [location] => stdClass Object
                        (
                            [lat] => 38.886672
                            [lng] => -77.094735
                        )

                    [accuracy] => 1
                    [accuracy_type] => rooftop
                    [source] => Arlington
                )

            [1] => stdClass Object
                (
                    [address_components] => stdClass Object
                        (
                            [number] => 1109
                            [predirectional] => N
                            [street] => Highland
                            [suffix] => St
                            [formatted_street] => N Highland St
                            [city] => Arlington
                            [county] => Arlington County
                            [state] => VA
                            [zip] => 22201
                            [country] => US
                        )

                    [formatted_address] => 1109 N Highland St, Arlington, VA 22201
                    [location] => stdClass Object
                        (
                            [lat] => 38.886665
                            [lng] => -77.094733
                        )

                    [accuracy] => 1
                    [accuracy_type] => rooftop
                    [source] => Virginia Geographic Information Network (VGIN)
                )

        )

)

如何访问 lat 和 lng 值?我很困惑,因为其中混合了对象和数组。我从来没有用 php 以这种方式处理过 json ......它是一个以 json 格式返回的地理编码 api。

我基本上想做类似 $lat = $result['results'][0]['location']['lat']; 之类的事情。

最佳答案

您必须使用 true 作为 json_decode 中的第二个参数,以便输出将是正常的 php 数组而不是 stdclass 数组(以便您可以使用您正在尝试的方法)

所以像下面这样: -

$result = json_decode($result,true);

echo $lat = $result['results'][0]['location']['lat'];
echo $lng = $result['results'][0]['location']['lng'];

注意:-

1. 使用您当前的格式,您也可以获得如下数据:-
echo $lat = $result->results[0]->location->lat; //using object approach
echo $lng = $result->results[0]->location->lng; //using object approach

2. 使用 foreach() 获取所有 lat-lng 记录。
foreach($result->results as $val) { //if using stdclass array
   echo "Latitude = ".$val->location->lat;
   echo "Longitude = ".$val->location->lng;
}

或者:-
foreach($result['results'] as $val) {//if using normal array
   echo "Latitude = ".$val['location']['lat'];
   echo "Longitude = ".$val['location']['lng'];
}

关于php解析json数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46658828/

10-12 13:04
查看更多