php - 地址对象中带有 PHP 的 Google API JSON 邮政编码

标签 php json google-maps-api-3

我想从 JSON 返回中获取地址详细信息,例如:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false

但是 Array 总是在元素数量上有所不同。那么知道如何使用 php 获取邮政编码等地址详细信息吗?

最佳答案

如果您想检索 postal_code,这应该对您有用。它应该让您了解如何访问您需要的其他数据:

// Decode json
$decoded_json = json_decode($json);

foreach($decoded_json->results as $results)
{

    foreach($results->address_components as $address_components)
    {
        // Check types is set then get first element (may want to loop through this to be safe,
        // rather than getting the first element all the time)
        if(isset($address_components->types) && $address_components->types[0] == 'postal_code')
        {
                    // Do what you want with data here
            echo $address_components->long_name;            
        }
    }
}

关于php - 地址对象中带有 PHP 的 Google API JSON 邮政编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21457677/

相关文章:

jquery - init 后用 JSON 填充 select2 不起作用

javascript - Google Maps Api v3 - map.panTo 导致 "too much recursion"

javascript - 从表单中删除具有值的输入元素会导致网站崩溃

gwt - Google map v3 for GWT : How to use map. setControls(JsArray<MVCArray<Node>> 控件)?

php - 在 PHP 中使用 cometd ?

php - MSBuild 复制特定文件和文件夹

javascript - 如何从html选择选项onChange调用php函数?

python - 如何使用 Django ORM 将 JSON 文件中的数据加载到 MySQL 实例中?

php - 准备好的语句 fetch_assoc php mysqli

javascript - 如何从两个数组在 JS 上构建动态对象