Trying to parse JSON with PHP

后端 未结 2 1979
情话喂你
情话喂你 2021-01-25 20:40

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\": {
        \         


        
2条回答
  •  礼貌的吻别
    2021-01-25 20:43

    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'];
    

    Demo

    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.

提交回复
热议问题