0

这似乎是一个新手问题,但我只是在这里用头撞键盘,我找不到任何已经回答的问题让我继续前进。

场景是我试图通过使用 Google Maps API 对其进行地理编码来获取邮政编码的 Lat/Lng。我已经从 Google Maps API 作为 JSON 字符串返回结果,并使用 json_decode 将其放入 PHP 数组中。但它看起来像是一个对象数组,我很难理解如何深入研究数据以获得我的 lat/lng 值。

这是当前的路障...代码然后结果:

<?php
$jsonData = '{"results":[{"address_components":[{"long_name":"33647","short_name":"33647","types":["postal_code"]},{"long_name":"Tampa","short_name":"Tampa","types":["locality","political"]},{"long_name":"Florida","short_name":"FL","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Tampa, FL 33647, USA","geometry":{"bounds":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}},"location":{"lat":28.14343180,"lng":-82.33433749999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}}},"types":["postal_code"]}],"status":"OK"}';
$phpArray = json_decode($jsonData);
print_r($phpArray);

foreach ($phpArray as $key => $value) { 
    echo "<p>$key | $value</p>";
}
?>

结果:

stdClass Object ( [results] => Array ( [0] => stdClass Object ( [address_components] => Array ( [0] => stdClass Object ( [long_name] => 33647 [short_name] => 33647 [types] => Array ( [0] => postal_code ) ) [1] => stdClass Object ( [long_name] => Tampa [short_name] => Tampa [types] => Array ( [0] => locality [1] => political ) ) [2] => stdClass Object ( [long_name] => Florida [short_name] => FL [types] => Array ( [0] => administrative_area_level_1 [1] => political ) ) [3] => stdClass Object ( [long_name] => United States [short_name] => US [types] => Array ( [0] => country [1] => political ) ) ) [formatted_address] => Tampa, FL 33647, USA [geometry] => stdClass Object ( [bounds] => stdClass Object ( [northeast] => stdClass Object ( [lat] => 28.1715 [lng] => -82.2623578 ) [southwest] => stdClass Object ( [lat] => 28.0729171 [lng] => -82.4256991 ) ) [location] => stdClass Object ( [lat] => 28.1434318 [lng] => -82.3343375 ) [location_type] => APPROXIMATE [viewport] => stdClass Object ( [northeast] => stdClass Object ( [lat] => 28.1715 [lng] => -82.2623578 ) [southwest] => stdClass Object ( [lat] => 28.0729171 [lng] => -82.4256991 ) ) ) [types] => Array ( [0] => postal_code ) ) ) [status] => OK )

results | Array

status | OK

用于创建输入 JSON 的 URL:

http://maps.googleapis.com/maps/api/geocode/json?address=33647&sensor=false

寻找一些帮助以将 Lat 和 Long 值提取到 PHP 变量中。

提前致谢!乔什

4

1 回答 1

1

results 的值实际上是另一个数组 - 因此您需要深入数组以获取所需的值。

此页面 (http://json.parser.online.fr/) 可能会帮助您更清楚地可视化数据。

这是一个用您的数据展示深度的可怕示例(数组作为值):

<?php
$jsonData = '{"results":[{"address_components":[{"long_name":"33647","short_name":"33647","types":["postal_code"]},{"long_name":"Tampa","short_name":"Tampa","types":["locality","political"]},{"long_name":"Florida","short_name":"FL","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Tampa, FL 33647, USA","geometry":{"bounds":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}},"location":{"lat":28.14343180,"lng":-82.33433749999999},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":28.17150,"lng":-82.26235779999999},"southwest":{"lat":28.07291710,"lng":-82.42569910}}},"types":["postal_code"]}],"status":"OK"}';
$phpArray = json_decode($jsonData,true);
print_r($phpArray);

foreach ($phpArray as $key => $value) {
    if ( $key == "results") {
        foreach ($value as $key2 => $value2) {
            foreach ($value2 as $key3 => $value3) {
                echo "<p>$key3 | $value3</p>";
            }
        }
    }
}
?>

您需要深入挖掘几个级别才能找到所需的所有数据。不过,这应该为您指明正确的方向。

于 2012-12-03T06:03:32.643 回答