use*_*954 3 php json google-maps-api-3
我想从 JSON 返回中获取地址详细信息,例如:
但数组的元素数量总是不同。那么有什么想法如何使用 php 获取邮政编码等地址详细信息吗?
如果您想检索邮政编码,这应该对您有用。它应该让您了解如何访问您需要的其他数据:
// 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)