我正在使用API来获取PHP中的某些数据。数据以JSON格式返回,使用json_decode我将此数据转换为数组。但是,我在循环和读取此数组时遇到困难。这是我的代码的一部分:
$content = json_decode($response);
foreach($content as $item) {
var_dump($item);
echo $item['id'];
echo $item['caption'];
}
回应示例:
object(stdClass)#1 (10) { ["_links"]=> object(stdClass)#2 (4) { ["self"]=> object(stdClass)#3 (1) { ["href"]=> string(48) "http://api.football-data.org/v1/competitions/444" } ["teams"]=> object(stdClass)#4 (1) { ["href"]=> string(54) "http://api.football-data.org/v1/competitions/444/teams" } ["fixtures"]=> object(stdClass)#5 (1) { ["href"]=> string(57) "http://api.football-data.org/v1/competitions/444/fixtures" } ["leagueTable"]=> object(stdClass)#6 (1) { ["href"]=> string(60) "http://api.football-data.org/v1/competitions/444/leagueTable" } } ["id"]=> int(444) ["caption"]=> string(33) "Campeonato Brasileiro da Série A" ["league"]=> string(3) "BSA" ["year"]=> string(4) "2017" ["currentMatchday"]=> int(38) ["numberOfMatchdays"]=> int(38) ["numberOfTeams"]=> int(20) ["numberOfGames"]=> int(380) ["lastUpdated"]=> string(20) "2017-12-11T07:05:27Z" }
完整数组的Var_dump:
[{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/444"},"teams":{"href":"http://api.football-data.org/v1/competitions/444/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/444/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/444/leagueTable"}},"id":444,"caption":"Campeonato Brasileiro da Série A","league":"BSA","year":"2017","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2017-12-11T07:05:27Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/445"},"teams":{"href":"http://api.football-data.org/v1/competitions/445/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/445/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/445/leagueTable"}},"id":445,"caption":"Premier League 2017/18","league":"PL","year":"2017","currentMatchday":32,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2018-03-21T08:10:10Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/446"},"teams":{"href":"http://api.football-data.org/v1/competitions/446/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/446/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/446/leagueTable"}},"id":446,"caption":"Championship 2017/18","league":"ELC","year":"2017","currentMatchday":39,"numberOfMatchdays":46,"numberOfTeams":24,"numberOfGames":552,"lastUpdated":"2018-03-21T08:10:16Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/447"},"teams":{"href":"http://api.football-data.org/v1/competitions/447/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/447/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/447/leagueTable"}},"id":447,"caption":"League One 2017/18","league":"EL1","year":"2017","currentMatchday":38,"numberOfMatchdays":46,"numberOfTeams":24,"numberOfGames":552,"lastUpdated":"2018-03-21T21:50:12Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/448"},"teams":{"href":"http://api.football-data.org/v1/competitions/448/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/448/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/448/leagueTable"}},"id":448,"caption":"League Two 2017/18","league":"EL2","year":"2017","currentMatchday":36,"numberOfMatchdays":46,"numberOfTeams":24,"numberOfGames":552,"lastUpdated":"2018-03-21T22:10:13Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/449"},"teams":{"href":"http://api.football-data.org/v1/competitions/449/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/449/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/449/leagueTable"}},"id":449,"caption":"Eredivisie 2017/18","league":"DED","year":"2017","currentMatchday":29,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2018-03-21T05:00:05Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/450"},"teams":{"href":"http://api.football-data.org/v1/competitions/450/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/450/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/450/leagueTable"}},"id":450,"caption":"Ligue 1 2017/18","league":"FL1","year":"2017","currentMatchday":31,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2018-03-21T11:00:10Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/451"},"teams":{"href":"http://api.football-data.org/v1/competitions/451/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/451/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/451/leagueTable"}},"id":451,"caption":"Ligue 2 2017/18","league":"FL2","year":"2017","currentMatchday":31,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2018-03-22T11:00:10Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/452"},"teams":{"href":"http://api.football-data.org/v1/competitions/452/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/452/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/452/leagueTable"}},"id":452,"caption":"1. Bundesliga 2017/18","league":"BL1","year":"2017","currentMatchday":28,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2018-03-21T08:10:30Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/453"},"teams":{"href":"http://api.football-data.org/v1/competitions/453/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/453/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/453/leagueTable"}},"id":453,"caption":"2. Bundesliga 2017/18","league":"BL2","year":"2017","currentMatchday":28,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2018-03-22T11:00:12Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/455"},"teams":{"href":"http://api.football-data.org/v1/competitions/455/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/455/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/455/leagueTable"}},"id":455,"caption":"Primera Division 2017","league":"PD","year":"2017","currentMatchday":30,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2018-03-21T11:00:15Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/456"},"teams":{"href":"http://api.football-data.org/v1/competitions/456/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/456/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/456/leagueTable"}},"id":456,"caption":"Serie A 2017/18","league":"SA","year":"2017","currentMatchday":30,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2018-03-21T11:00:16Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/457"},"teams":{"href":"http://api.football-data.org/v1/competitions/457/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/457/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/457/leagueTable"}},"id":457,"caption":"Primeira Liga 2017/18","league":"PPL","year":"2017","currentMatchday":28,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2018-03-21T11:00:17Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/458"},"teams":{"href":"http://api.football-data.org/v1/competitions/458/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/458/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/458/leagueTable"}},"id":458,"caption":"DFB-Pokal 2017/18","league":"DFB","year":"2017","currentMatchday":5,"numberOfMatchdays":6,"numberOfTeams":64,"numberOfGames":62,"lastUpdated":"2018-02-11T20:00:16Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/459"},"teams":{"href":"http://api.football-data.org/v1/competitions/459/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/459/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/459/leagueTable"}},"id":459,"caption":"Serie B 2017/18","league":"SB","year":"2017","currentMatchday":32,"numberOfMatchdays":42,"numberOfTeams":22,"numberOfGames":462,"lastUpdated":"2018-03-22T11:00:18Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/464"},"teams":{"href":"http://api.football-data.org/v1/competitions/464/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/464/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/464/leagueTable"}},"id":464,"caption":"Champions League 2017/18","league":"CL","year":"2017","currentMatchday":8,"numberOfMatchdays":10,"numberOfTeams":32,"numberOfGames":120,"lastUpdated":"2018-03-19T20:10:15Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/466"},"teams":{"href":"http://api.football-data.org/v1/competitions/466/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/466/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/466/leagueTable"}},"id":466,"caption":"Australian A-League","league":"AAL","year":"2017","currentMatchday":24,"numberOfMatchdays":27,"numberOfTeams":10,"numberOfGames":135,"lastUpdated":"2018-03-20T20:00:17Z"}]
第一个问题是它只返回数组的第一项。由于某种原因,它不会循环第二,第三等项目。
此外,我可以看到var_dump数组包含数据,但是当我尝试获取某些字段(如id和caption)时,它只返回一个空字符串。
我一直试图让它正确一段时间,但有点卡住了。希望有人能给我一个正确的方向。
答案 0 :(得分:1)
json_decode采用JSON编码的字符串并将其转换为PHP变量。 但是如果将assoc设置为TRUE,则返回转换为关联数组的对象。
$replacements
答案 1 :(得分:0)
我在我这边工作过,json包含一个特殊的chareacter,正确的代码在我提到的下面。
$content = '[{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/444"},"teams":{"href":"http://api.football-data.org/v1/competitions/444/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/444/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/444/leagueTable"}},"id":444,"caption":"Campeonato Brasileiro da Serie A","league":"BSA","year":"2017","currentMatchday":38,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2017-12-11T07:05:27Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/445"},"teams":{"href":"http://api.football-data.org/v1/competitions/445/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/445/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/445/leagueTable"}},"id":445,"caption":"Premier League 2017/18","league":"PL","year":"2017","currentMatchday":32,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2018-03-21T08:10:10Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/446"},"teams":{"href":"http://api.football-data.org/v1/competitions/446/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/446/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/446/leagueTable"}},"id":446,"caption":"Championship 2017/18","league":"ELC","year":"2017","currentMatchday":39,"numberOfMatchdays":46,"numberOfTeams":24,"numberOfGames":552,"lastUpdated":"2018-03-21T08:10:16Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/447"},"teams":{"href":"http://api.football-data.org/v1/competitions/447/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/447/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/447/leagueTable"}},"id":447,"caption":"League One 2017/18","league":"EL1","year":"2017","currentMatchday":38,"numberOfMatchdays":46,"numberOfTeams":24,"numberOfGames":552,"lastUpdated":"2018-03-21T21:50:12Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/448"},"teams":{"href":"http://api.football-data.org/v1/competitions/448/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/448/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/448/leagueTable"}},"id":448,"caption":"League Two 2017/18","league":"EL2","year":"2017","currentMatchday":36,"numberOfMatchdays":46,"numberOfTeams":24,"numberOfGames":552,"lastUpdated":"2018-03-21T22:10:13Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/449"},"teams":{"href":"http://api.football-data.org/v1/competitions/449/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/449/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/449/leagueTable"}},"id":449,"caption":"Eredivisie 2017/18","league":"DED","year":"2017","currentMatchday":29,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2018-03-21T05:00:05Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/450"},"teams":{"href":"http://api.football-data.org/v1/competitions/450/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/450/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/450/leagueTable"}},"id":450,"caption":"Ligue 1 2017/18","league":"FL1","year":"2017","currentMatchday":31,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2018-03-21T11:00:10Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/451"},"teams":{"href":"http://api.football-data.org/v1/competitions/451/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/451/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/451/leagueTable"}},"id":451,"caption":"Ligue 2 2017/18","league":"FL2","year":"2017","currentMatchday":31,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2018-03-22T11:00:10Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/452"},"teams":{"href":"http://api.football-data.org/v1/competitions/452/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/452/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/452/leagueTable"}},"id":452,"caption":"1. Bundesliga 2017/18","league":"BL1","year":"2017","currentMatchday":28,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2018-03-21T08:10:30Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/453"},"teams":{"href":"http://api.football-data.org/v1/competitions/453/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/453/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/453/leagueTable"}},"id":453,"caption":"2. Bundesliga 2017/18","league":"BL2","year":"2017","currentMatchday":28,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2018-03-22T11:00:12Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/455"},"teams":{"href":"http://api.football-data.org/v1/competitions/455/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/455/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/455/leagueTable"}},"id":455,"caption":"Primera Division 2017","league":"PD","year":"2017","currentMatchday":30,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2018-03-21T11:00:15Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/456"},"teams":{"href":"http://api.football-data.org/v1/competitions/456/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/456/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/456/leagueTable"}},"id":456,"caption":"Serie A 2017/18","league":"SA","year":"2017","currentMatchday":30,"numberOfMatchdays":38,"numberOfTeams":20,"numberOfGames":380,"lastUpdated":"2018-03-21T11:00:16Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/457"},"teams":{"href":"http://api.football-data.org/v1/competitions/457/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/457/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/457/leagueTable"}},"id":457,"caption":"Primeira Liga 2017/18","league":"PPL","year":"2017","currentMatchday":28,"numberOfMatchdays":34,"numberOfTeams":18,"numberOfGames":306,"lastUpdated":"2018-03-21T11:00:17Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/458"},"teams":{"href":"http://api.football-data.org/v1/competitions/458/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/458/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/458/leagueTable"}},"id":458,"caption":"DFB-Pokal 2017/18","league":"DFB","year":"2017","currentMatchday":5,"numberOfMatchdays":6,"numberOfTeams":64,"numberOfGames":62,"lastUpdated":"2018-02-11T20:00:16Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/459"},"teams":{"href":"http://api.football-data.org/v1/competitions/459/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/459/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/459/leagueTable"}},"id":459,"caption":"Serie B 2017/18","league":"SB","year":"2017","currentMatchday":32,"numberOfMatchdays":42,"numberOfTeams":22,"numberOfGames":462,"lastUpdated":"2018-03-22T11:00:18Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/464"},"teams":{"href":"http://api.football-data.org/v1/competitions/464/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/464/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/464/leagueTable"}},"id":464,"caption":"Champions League 2017/18","league":"CL","year":"2017","currentMatchday":8,"numberOfMatchdays":10,"numberOfTeams":32,"numberOfGames":120,"lastUpdated":"2018-03-19T20:10:15Z"},{"_links":{"self":{"href":"http://api.football-data.org/v1/competitions/466"},"teams":{"href":"http://api.football-data.org/v1/competitions/466/teams"},"fixtures":{"href":"http://api.football-data.org/v1/competitions/466/fixtures"},"leagueTable":{"href":"http://api.football-data.org/v1/competitions/466/leagueTable"}},"id":466,"caption":"Australian A-League","league":"AAL","year":"2017","currentMatchday":24,"numberOfMatchdays":27,"numberOfTeams":10,"numberOfGames":135,"lastUpdated":"2018-03-20T20:00:17Z"}]';
$data = json_decode($content, true);
foreach($data as $item){
echo $item['id'];
}