【发布时间】:2015-12-19 23:11:01
【问题描述】:
我最近问了一个关于如何解析这个 JSON 提要的问题。给出了答案,但它也提出了另一个问题。回声正在为提要中的每个玩家吐出重复的记录。我不确定为什么会这样,我希望有人可以帮助我。
这是我的代码:
$url = file_get_contents("http://www.nfl.com/liveupdate/game-center/2015091700/2015091700_gtd.json");
$json = json_decode($url, true); // 'true' makes data an array
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json));
$player = array();
foreach($iterator as $key=>$value) {
$player[$key] = $value;
echo $player['name'] . ' ' . $player['att'] . ' ' . $player['cmp'] . ' ' . $player['yds'] . ' ' . $player['tds'] . ' ' . $player['fgm'] . ' ' . $player['fga'] . '<br>';
}
这是 JSON:
{
"2015091700":{
"home":{
"abbr":"KC",
"to":0,
"stats":{
"passing":{
"00-0023436":{
"name":"A.Smith",
"att":25,
"cmp":16,
"yds":191,
"tds":0,
"ints":2
}
},
"rushing":{
"00-0026213":{
"name":"J.Charles",
"att":21,
"yds":125,
"tds":1
}
}
}
}
}
}
它给了我重复。见下文。
A.Smith
A.Smith 25
A.Smith 25 16
A.Smith 25 16 191
A.Smith 25 16 191 0
A.Smith 25 16 191 0
A.Smith 25 16 191 0
A.Smith 25 16 191 0
J.Charles 25 16 191 0
J.Charles 21 16 191 0
J.Charles 21 16 125 0
J.Charles 21 16 125 1
J.Charles 21 16 125 1
J.Charles 21 16 125 1
J.Charles 21 16 125 1
J.Charles 21 16 125 1
我希望每个玩家都有独特的结果。
A.Smith 应该是 A.Smith 25 16 191 0 2 而 J.Charles 应该是 J.Charles 21 125 1 而不是您在上面看到的。
【问题讨论】:
-
你想要什么结果?
-
我希望每个玩家的统计数据都能反映提要。所以 A.Smith 25 16 191 0 和 J.Charles 21 125 1。我将每个值设置为变量,然后将其插入到我的数据库表中。我更新了我的问题以包含 JSON。
标签: php arrays json parsing duplicates