访问数组中的数据


Access data in an array

我想做的是阅读信息并能够获得每场比赛的数据,例如哪些球队参加了比赛,谁赢了等等。我已经尝试了所有方法,但似乎无法做到这一点。

这是我的数据结构:

stdClass Object (
    [_links] => Array (
        [0] => stdClass Object (
        [self] => http://api.football-data.org/alpha/soccerseasons/354/fixtures 
        )
        [1] => stdClass Object (
        [soccerseason] => http://api.football-data.org/alpha/soccerseasons/354 
        ) 
    ) 
    [count] => 10 
    [fixtures] => Array (
        [0] => stdClass Object (
            [_links] => stdClass Object (
                [self] => stdClass Object (
                    [href] => http://api.football-data.org/alpha/fixtures/137842 
                    ) 
                    [soccerseason] => stdClass Object (
                    [href] => http://api.football-data.org/alpha/soccerseasons/354 
                    ) 
                    [homeTeam] => stdClass Object (
                    [href] => http://api.football-data.org/alpha/teams/338 
                    ) 
                    [awayTeam] => stdClass Object (
                    [href] => http://api.football-data.org/alpha/teams/70 
                    ) 
            ) 
            [date] => 2015-01-17T15:00:00Z 
            [status] => FINISHED 
            [matchday] => 22 
            [homeTeamName] => Leicester City 
            [awayTeamName] => Stoke City FC 
            [result] => stdClass Object (
                [goalsHomeTeam] => 0 
                [goalsAwayTeam] => 1 
            )
        )
        [1] => stdClass Object (
            [_links] => stdClass Object (
                [self] => stdClass Object (
                    [href] => http://api.football-data.org/alpha/fixtures/136840 
                    )
                    [soccerseason] => stdClass Object (
                    [href] => http://api.football-data.org/alpha/soccerseasons/354 
                    ) 
                    [homeTeam] => stdClass Object (
                    [href] => http://api.football-data.org/alpha/teams/72 
                    ) 
                    [awayTeam] => stdClass Object (
                    [href] => http://api.football-data.org/alpha/teams/61 
                    )
            )
            [date] => 2015-01-17T15:00:00Z 
            [status] => FINISHED 
            [matchday] => 22 
            [homeTeamName] => Swansea City 
            [awayTeamName] => Chelsea FC 
            [result] => stdClass Object (
                [goalsHomeTeam] => 0 
                [goalsAwayTeam] => 5 
                )
            )
    )
)

例如:我想知道"主队名称"、"客场队名"、"进球主队"、"进球客队"的值是多少......

很难说,因为您没有发布正在使用的代码,但我怀疑您混淆了结构类型。您发布的实际上是一个对象 - stdClass的实例,看起来像数组元素的项目实际上是该对象的属性。因此,您需要使用适当的访问方法->propertyName而不是['propertyName']

// YES
echo $data->fixtures[0]->homeTeamName;
// NO
echo $data['fixtures'][0]['homeTeamName'];

因此,要获取每个游戏(或我认为是"游戏"项目)的数据,您需要执行以下操作:

foreach ($data->fixtures as $game) {
    echo $game->homeTeamName;
    echo $game->awayTeamName;
    echo $game->result->goalsHomeTeam;
    echo $game->result->goalsAwayTeam;
    // etc..
}

这似乎也是使用 json_decode() 解析的 API 的返回值。如果是这种情况,您实际上可以通过true作为第二个参数传递给json_decode()来使其成为数组:

$data = json_decode($response, true);
foreach ($data['fixtures'] as $game) {
    echo $game['homeTeamName'];
    echo $game['awayTeamName'];
    echo $game['result']['goalsHomeTeam'];
    echo $game['result']['goalsAwayTeam'];
    // etc..
}