获得多个密钥'来自JSON和PHP的值

时间:2015-10-24 09:16:00

标签: php json

我正在尝试从这个json中提取球队名称,球员名字。但没有结果。

我的json文件看起来像。我知道哪里错了?

{"items":[
{"_id":305501,"id":"305501","created_at":"2015-10-19T19:57:02+13:00","updated_at":"2015-10-19T21:34:50+13:00","name":"Match in progress","verification_level":0,
    "club_one":{"name":"Degree Club","id":14748,
        "team":{"name":"Degree College XI","id":13009,"avatar":"/original/team/default_thumb.png",
            "players":[
            {"id":null,"name":"D Vinaya"},
            {"id":617744,"name":"V Avika"},
            {"id":617745,"name":"C Rumes"},                         
            {"id":1360372,"name":"R Ferdo"}
            ],
            "innings":[{"overs":34,"over_balls":0,"runs":99,"wickets":7}]
        }
    },
    "club_two":{"name":"George Club","id":147736,
        "team":{"name":"George College XI","id":154503,"avatar":"/original/team/default_thumb.png",
            "players":[
            {"id":null,"name":"M Premathe†"},
            {"id":null,"name":"S Tion"},
            {"id":null,"name":"N Perra"},               
            {"id":1400317,"name":"S Ren"}
            ],
            "innings":[]
        }
    },
"processed":true,"visible":true,"match_level":{"name":null,"id":null}   
}   
],
"meta":{"total_pages":1}
}

这是php代码。

$json_file = json_decode($load_json);
           foreach ($items as $item) {                
               echo $item->$json_file->_id;
                echo $item->$json_file->club_one;
                echo $item->$json_file->club_two;
                   }

1 个答案:

答案 0 :(得分:1)

你做错了。就你的json而言,像这样循环

$json_file = json_decode($json);
foreach ($json_file->items as $item) {                
    echo $item->id;
    echo $item->club_one->name;
    echo $item->club_two->name;
}