警告:为foreach()Json提供的参数无效

时间:2015-08-03 12:28:01

标签: php json

我正在尝试反序列化以下json。

{
    "_links":{
    "self":"http://api.football-data.org/alpha/soccerseasons/403/leagueTable/?matchday=2","soccerseason":"http://api.football-data.org/alpha/soccerseasons/403"},"leagueCaption":"3. Bundesliga 2015/16","matchday":2,"standing":[{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/23"}},"position":1,"teamName":"Energie Cottbus","playedGames":2,"points":6,"goals":4,"goalsAgainst":0,"goalDifference":4},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/740"}},"position":2,"teamName":"Mainz 05 II","playedGames":2,"points":4,"goals":6,"goalsAgainst":2,"goalDifference":4},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/35"}},"position":3,"teamName":"Dynamo Dresden","playedGames":2,"points":4,"goals":5,"goalsAgainst":2,"goalDifference":3},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/51"}},"position":4,"teamName":"Preußen Münster","playedGames":2,"points":4,"goals":4,"goalsAgainst":2,"goalDifference":2},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/1054"}},"position":5,"teamName":"Magdeburg","playedGames":2,"points":4,"goals":4,"goalsAgainst":3,"goalDifference":1},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/202"}},"position":6,"teamName":"Stuttgarter Kickers","playedGames":2,"points":4,"goals":3,"goalsAgainst":2,"goalDifference":1},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/1053"}},"position":7,"teamName":"Werder Bremen II","playedGames":2,"points":3,"goals":2,"goalsAgainst":3,"goalDifference":-1},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/1055"}},"position":8,"teamName":"Würzburger Kickers","playedGames":2,"points":2,"goals":1,"goalsAgainst":1,"goalDifference":0},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/52"}},"position":8,"teamName":"VfL Osnabrück","playedGames":2,"points":2,"goals":1,"goalsAgainst":1,"goalDifference":0},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/50"}},"position":8,"teamName":"VfR Aalen","playedGames":2,"points":2,"goals":1,"goalsAgainst":1,"goalDifference":0},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/54"}},"position":8,"teamName":"Chemnitzer FC","playedGames":2,"points":2,"goals":1,"goalsAgainst":1,"goalDifference":0},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/39"}},"position":12,"teamName":"SV Wehen Wiesbaden","playedGames":2,"points":2,"goals":0,"goalsAgainst":0,"goalDifference":0},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/741"}},"position":13,"teamName":"SG Sonnenhof Großaspach","playedGames":1,"points":1,"goals":1,"goalsAgainst":1,"goalDifference":0},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/22"}},"position":14,"teamName":"Erzgebirge Aue","playedGames":1,"points":1,"goals":0,"goalsAgainst":0,"goalDifference":0},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/34"}},"position":15,"teamName":"FC Hansa Rostock","playedGames":2,"points":1,"goals":2,"goalsAgainst":3,"goalDifference":-1},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/204"}},"position":15,"teamName":"Fortuna Köln","playedGames":2,"points":1,"goals":2,"goalsAgainst":3,"goalDifference":-1},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/40"}},"position":17,"teamName":"Rot-Weiß Erfurt","playedGames":2,"points":1,"goals":1,"goalsAgainst":2,"goalDifference":-1},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/554"}},"position":18,"teamName":"Hallescher FC","playedGames":1,"points":0,"goals":0,"goalsAgainst":2,"goalDifference":-2},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/720"}},"position":19,"teamName":"Holstein Kiel","playedGames":1,"points":0,"goals":0,"goalsAgainst":4,"goalDifference":-4},{
    "_links":{
        "team":{
            "href":"http://api.football-data.org/alpha/teams/45"}},"position":20,"teamName":"VfB Stuttgart II","playedGames":2,"points":0,"goals":2,"goalsAgainst":7,"goalDifference":-5}]}

但是foreach出了点问题。

    $variable = file_get_contents("http://api.football-data.org/alpha/soccerseasons/403/leagueTable");
    $decoded = json_decode($variable, true);

var_dump($decoded);
foreach($decoded['leagueCaption'] as $leagueCaption)
{...}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

你与foreach参数混淆了

$variable = file_get_contents("http://api.football-data.org/alpha/soccerseasons/403/leagueTable");
$decoded = json_decode($variable, true);

var_dump($decoded);
foreach($decoded['standing'] as $leagueCaption){

}

leagueCaption只是一个字符串“3. Bundesliga 2015/16”