dongtangdao7232 2015-05-31 09:21
浏览 18
已采纳

使用PHP解析JSON(带选项卡和数组的数组)

I have been trying to parse a JSON response since few days without success. I've read on the internet and this site how to parse with a foreach but I think the structure of json is too different from the examples I've found. So here is the JSON:

 array(1) 

        { ["matches"]=> array(10) 
            { [0]=> array(13) { ["matchId"]=> int(2125878813) 
                                ["region"]=> string(3) "EUW" 
                                ["platformId"]=> string(4) "EUW1" 
                                ["matchMode"]=> string(7) "CLASSIC" 
                                ["matchType"]=> string(12) "MATCHED_GAME" 
                                ["matchCreation"]=> float(1432656547642) 
                                ["matchDuration"]=> int(1852) 
                                ["queueType"]=> string(15) "RANKED_SOLO_5x5" 
                                ["mapId"]=> int(11) 
                                ["season"]=> string(10) "SEASON2015" 
                                ["matchVersion"]=> string(9) "5.9.0.318" 
                                ["participants"]=> array(1) 
                                    { [0]=> array(10) { 
                                    ["teamId"]=> int(200) 
                                    ["spell1Id"]=> int(7) 
                                    ["spell2Id"]=> int(4) 
                                    ["championId"]=> int(67) 
                                    ["highestAchievedSeasonTier"]=> string(8) "PLATINUM" 
                                    ["timeline"]=> array(9) 
                                        { ["creepsPerMinDeltas"]=> array(3) 
                                            { ["zeroToTen"]=> float(6.2) 
                                              ["tenToTwenty"]=> float(7.4) 
                                              ["twentyToThirty"]=> float(4.4) } 
                                              ["xpPerMinDeltas"]=> array(3) 
                                                { ["zeroToTen"]=> float(336.2) 
                                                  ["tenToTwenty"]=> float(428.3) 
                                                  ["twentyToThirty"]=> float(396) } 
                                                    ["goldPerMinDeltas"]=> array(3) { 
                                                    ["zeroToTen"]=> float(270.7) 
                                                    ["tenToTwenty"]=> float(363.6) 
                                                    ["twentyToThirty"]=> float(294.8) }

If I understand well this is an array with tabs inside and arrays also ?

I would like for example to display the 10 matches id : matches->matchId matches->participants->championId matches->participants->timeline->creepsPerMinDeltas->zeroToTen

Here is what I've done so far to display the matchId :

 $decodeHisto = json_decode ($resultHisto,true);

    //var_dump($decodeHisto);       
    foreach ($decodeHisto{[matches]} as $d)      
    {

    echo $d->{[matchId]}-;

    }

Can anyone help me please ?

  • 写回答

2条回答 默认 最新

  • dousi6701 2015-05-31 09:34
    关注

    Your code is not JSON, as JSON is exactly defined like this:

    {
    "Herausgeber": "Xema",
      "Nummer": "1234-5678-9012-3456",
      "Deckung": 2e+6,
      "Waehrung": "EURO",
      "Inhaber": {
        "Name": "Mustermann",
        "Vorname": "Max",
        "maennlich": true,
        "Hobbys": [ "Reiten", "Golfen", "Lesen" ],
        "Alter": 42,
        "Kinder": [],
        "Partner": null
      }
    }
    

    You can convert a JSON array to PHP with this code:

    $array = json_decode($json);
    

    But as I said before, your code is not JSON. It seems to be even a PHP array. Then you can access with:

    $arr["matches"][0]["matchId"]
    

    for example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式