尝试使用PHP解析JSON [英] Trying to parse JSON with PHP

查看:78
本文介绍了尝试使用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.

这篇关于尝试使用PHP解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆