尝试使用PHP解析JSON
我是php的新手,这真的使我感到难过-我试图解析json以获取match_id
的值.
I'm new to php and this has really stumped me - i'm trying to parse this json in order to get the value of match_id
.
{
"result": {
"status": 1,
"num_results": 1,
"total_results": 500,
"results_remaining": 499,
"matches": [
{
"match_id": 649218382,
"match_seq_num": 588750904,
"start_time": 1399560988,
"lobby_type": 0,
"players": [
{
"account_id": 4294967295,
"player_slot": 0,
"hero_id": 69
}
]
}
]
}
}
到目前为止,我有:
$matchhistoryjson = file_get_contents($apimatchhistoryurl);
$decodedmatchhistory = json_decode($matchhistoryjson, true);
$matchid = $decodedmatchhistory->{'match_id'};
但是我很确定这根本不是正确的方法.我只需要这个JSON文件就是匹配ID.
But I'm pretty sure that's not the right way to do it at all. All I need out of this JSON file is the match id.
当您通过第二个参数传递一个值为true
的值时,您正在从json_decode()
返回一个数组,因此您可以像访问任何多维数组一样对其进行访问
数组:
You are getting an array back from json_decode()
as you passed the second parameter with a value of true
so you access it like any multi-dimensional
array:
$matchhistoryjson = file_get_contents($apimatchhistoryurl);
$decodedmatchhistory = json_decode($matchhistoryjson, true);
echo $decodedmatchhistory['result']['matches'][0]['match_id'];
自然地,如果您有多个匹配项,则希望获得匹配ID,因为您可以遍历$decodedmatchhistory['result']['matches']
并相应地获取它们.
Naturally if you have multiple matches you wish to get the match ID for you can loop through $decodedmatchhistory['result']['matches']
and get them accordingly.