数组里面的对象怎么获取
首先,我得到的是一个string:
{"status":"0","t":"","set_cache_time":"","data":[{"ExtendedLocation":"","OriginQuery":"113.219.202.173","appinfo":"","disp_type":0,"fetchkey":"113.219.202.173","location":"湖南省 电信","origip":"113.219.202.173","origipquery":"113.219.202.173","resourceid":"6006","role_id":0,"shareImage":1,"showLikeShare":1,"showlamp":"1","titlecont":"IP地址查询","tplt":"ip"}]}
然后我json_decode($res->body())
一下,得到:
{
"status": "0",
"t": "",
"set_cache_time": "",
"data": [
{
"ExtendedLocation": "",
"OriginQuery": "113.219.202.173",
"appinfo": "",
"disp_type": 0,
"fetchkey": "113.219.202.173",
"location": "湖南省 电信",
"origip": "113.219.202.173",
"origipquery": "113.219.202.173",
"resourceid": "6006",
"role_id": 0,
"shareImage": 1,
"showLikeShare": 1,
"showlamp": "1",
"titlecont": "IP地址查询",
"tplt": "ip"
}
]
}
然后我json_decode($res->body())->data
一下,得到:
[
{
"ExtendedLocation": "",
"OriginQuery": "39.144.145.92",
"appinfo": "",
"disp_type": 0,
"fetchkey": "39.144.145.92",
"location": "中国 移动",
"origip": "39.144.145.92",
"origipquery": "39.144.145.92",
"resourceid": "6006",
"role_id": 0,
"shareImage": 1,
"showLikeShare": 1,
"showlamp": "1",
"titlecont": "IP地址查询",
"tplt": "ip"
}
]
最后我只想要location
,但是,无论我怎么写都不对
我写json_decode($res->body())->data[0]
:
报错:
Undefined array key 0
我写json_decode($res->body())->data->location
:
报错:
Attempt to read property "location" on array
所以我到底要写啥?不就是数组里的对象吗?
补充一下代码:
$grid->column('ip_address','ip地址')->display(function($v){
$res = Http::get('http://opendata.baidu.com/api.php?query=' . $v. '&co=&resource_id=6006&oe=utf8');
$location = json_decode($res->body())->data;
return $location;
});