【问题标题】:Duplicates in Parsed JSON解析的 JSON 中的重复项
【发布时间】: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


【解决方案1】:

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

一个快速但有点脏的解决方案是在新播放器启动时保存并重置。这里我们假设“name”键始终存在并且始终是第一个条目。

$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 的明确定义和已知格式。

【讨论】:

  • 这非常有效。太感谢了。非常感谢您的解释。
猜你喜欢
  • 2011-04-12
  • 2014-01-06
  • 1970-01-01
  • 1970-01-01
  • 2020-08-04
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多