【问题标题】:Use PHP to read an array of objects from Google Maps API使用 PHP 从 Google Maps API 读取对象数组
【发布时间】:2012-11-20 14:17:40
【问题描述】:

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

场景是我试图通过使用 Google Maps API 对其进行地理编码来获取邮政编码的纬度/经度。我已经从 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 变量中。

提前致谢! 乔什

【问题讨论】:

    标签: php google-maps-api-3


    【解决方案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>";
                }
            }
        }
    }
    ?>
    

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

    【讨论】:

    • 感谢 Russ... 这确实让我走上了正确的道路。作为另一种选择,我挖得更深一点,发现这有助于我深入研究一个或多个数组:-) $fullurl = "maps.googleapis.com/maps/api/geocode/…"; $string = file_get_contents($fullurl); // 获取 json 内容 $json_a = json_decode($string, true); //json解码器回显 $json_a['results'][0]['geometry']['location']['lat']; echo $json_a['results'][0]['geometry']['location']['lng'];
    猜你喜欢
    • 2011-10-26
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多