在Parsed JSON中重复

时间:2015-09-22 20:08:58

标签: php arrays json parsing duplicates

我最近问了一个关于如何解析这个JSON提要的问题。给出了答案,但它也提出了另一个问题。 echo正在为Feed中的每个玩家吐出重复记录。我不确定为什么会这样,我希望有人可以帮助我。

这是我的代码:

$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而不是你上面看到的。

1 个答案:

答案 0 :(得分:1)

代码实际上并没有创建重复项,您只需打印出每个间歇性结果。实际上,循环在每次迭代中都会覆盖现有键的数据。如果您只有一个玩家的数据,这可能会有效,但在这种情况下可能导致多个玩家的意外(错误)结果。

一个快速而有点脏的解决方案是在新玩家启动时保存并重置。在这里,我们假设&#39;名称&#39; key始终存在,始终是第一个条目。

$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>';
}

直接从解析数组中获取数据会更清晰,但这需要为json定义明确且已知的格式。