douzhuijing4911 2018-03-23 11:22
浏览 53
已采纳

JSON解码数组仅显示第一项而没有值

I am using an API to get certain data in PHP. The data is returned in JSON and with json_decode I convert this data to an array. However, I am having difficulties looping and reading this array. Here is part of my code:

$content = json_decode($response);

foreach($content as $item) {

  var_dump($item);

  echo $item['id'];
  echo $item['caption'];

}

Response example:

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 of the full array:

[{"_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"}]

First problem is that it is only returning the first item of the array. For some reason it does not loop the second, third, etc. item.

Also I can see with the var_dump that the array contains data, but when I try to get certain fields like id and caption it is just returning an empty string.

I have been trying to get it right for some time but am kinda stuck. Hope someone can give me a push in the right direction.

  • 写回答

2条回答 默认 最新

  • dqsh30374 2018-03-23 11:30
    关注

    json_decode takes a JSON encoded string and converts it into a PHP variable. but with assoc set to TRUE it return objects converted into associative arrays.

    $content = json_decode($response, true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化