Reputation: 1082
I recently asked a question about how to parse this JSON feed. An answer was given but it also presented another problem. The echo is spitting out duplicate records for each player in the feed. I'm not sure why this is happening and I hope someone can help me out.
Here's my code:
$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>';
}
This is the 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
}
}
}
}
}
}
It is giving me duplicates. See below.
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
I would like unique results for each player.
A.Smith should be A.Smith 25 16 191 0 2 and J.Charles should be J.Charles 21 125 1 instead of what you see above.
Upvotes: 1
Views: 84
Reputation: 474
The code is not actually creating duplicates, you just print out every intermittent result. In fact, the loop is overwriting data for existing keys in every iteration. This would work if you only have data for one player, but can lead to unexpected (wrong) results in this case with multiple players.
A quick and somewhat dirty solution would be to save and reset when a new player is started. Here we assume the 'name' key is always present and always the first entry.
$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));
$players = array();
$player = false;
foreach ( $iterator as $key => $value ) {
// The key 'name' marks the start of a new player
if ( $key == 'name' ) {
// If we were already processing a player, save it
if ( is_array($player) ) {
$players[] = $player;
}
// Start a new player
$player = array();
}
// If we are processing a player, save the values
if ( is_array($player) ) {
$player[$key] = $value;
}
}
// Don't forget the last player
$players[] = $player;
// Output the resulting players
print_r($players);
// Or, in your desired output format
// Will give a ton of E_NOTICES on dev setups!
foreach( $players as $player ) {
echo $player['name'] . ' ' . $player['att'] . ' ' . $player['cmp'] . ' ' . $player['yds'] . ' ' . $player['tds'] . ' ' . $player['fgm'] . ' ' . $player['fga'] . '<br>';
}
It would be cleaner to actually get the data from the parsed array directly, but this requires a well defined and known format for the json.
Upvotes: 1