gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:44:38 26 4
gpt4 key购买 nike

我想从 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com