我想要做的是阅读信息,并能够获取每个游戏的数据,以及哪些团队参加比赛,谁赢了等等。我已经尝试过所有事情,但似乎无法做到这一点。
这是我的数据结构:
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
)
)
)
)
例如:我想知道" homeTeamName"," awayTeamName"," goalsHomeTeam"," goalsAwayTeam&的价值#34; ...
答案 0 :(得分:0)
很难说,因为你没有发布你正在使用的代码,但我怀疑你是混淆了结构类型。您发布的内容实际上是一个对象 - stdClass
的一个实例,看起来像数组元素的项实际上是该对象的属性。因此,您需要使用->propertyName
的相应访问方法,而不是['propertyName']
:
// YES
echo $data->fixtures[0]->homeTeamName;
// NO
echo $data['fixtures'][0]['homeTeamName'];
为了获得每个游戏的数据(或者我认为是#34;游戏"项目),你会这样做:
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..
}