我想从 JSON 返回中获取地址详细信息,例如:
但是 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/