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 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误