我在PHP中有一个变量 $ output ,里面有这个:
{
"results": [
{
"address_components": [
{
"long_name": "6",
"short_name": "6",
"types": [
"street_number"
]
},
{
"long_name": "",
"short_name": "",
"types": [
"route"
]
},
{
"long_name": "",
"short_name": "",
"types": [
"locality",
"political"
]
},
{
"long_name": "Barcelona",
"short_name": "Barcelona",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Catalunya",
"short_name": "CT",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Spain",
"short_name": "ES",
"types": [
"country",
"political"
]
},
{
"long_name": "0830",
"short_name": "0830",
"types": [
"postal_code"
]
}
],
"formatted_address": "Barcelona, Spain",
"status": "OK"
}
我想访问“formatted_address”中的数据,并将其值存储在PHP中的变量中,以便稍后将其存储在我的数据库中。我尝试了很多东西,但没有成功。
这是我试过的:
$info= json_decode($output);
echo $info->results;
echo $info->results[0];
echo $info.results;
echo $info['results']
etc... etc...
您能否告诉我如何在PHP中访问此信息?
请帮忙!
答案 0 :(得分:1)
答案 1 :(得分:0)
或者作为一行:
echo json_decode($output)->results[0]->formatted_address;