地址对象中包含 PHP 的 Google API JSON 邮政编码

use*_*954 3 php json google-maps-api-3

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

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

但数组的元素数量总是不同。那么有什么想法如何使用 php 获取邮政编码等地址详细信息吗?

Spr*_*gie 5

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

// 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;            
        }
    }
}
Run Code Online (Sandbox Code Playgroud)