【问题标题】:Push data to a parent array from within child array (PHP)从子数组(PHP)中将数据推送到父数组
【发布时间】:2020-05-17 19:19:36
【问题描述】:

我有一系列棒球队,每个棒球队都有一系列球队的赛季。每个赛季内是该赛季的获胜次数。我想统计一个团队的所有胜利,然后将这个数字推送到每个团队的数据中。我的最终目标是按照每支球队的总胜场数对这个主数组进行排序。

我的问题是,我不知道如何将 totalGamesWon 计数推到团队数据的主数组中。

对于效率低下的编码提前道歉!如果像我这样的新手有更好的方法来做到这一点,我会全力以赴!

$teams = array(
  array (
    'teamName' => 'Yankees',
    'seasons' => array (
      array(
        'seasonName' => '2018 Regular Season',
        'wins' => 100
      ),
      array(
        'seasonName' => '2018 Playoffs',
        'wins' => 2
      )
    )
  ),
  array (
    'teamName' => 'Red Sox',
    'seasons' => array (
      array(
        'seasonName' => '2018 Regular Season',
        'wins' => 108
      ),
      array(
        'seasonName' => '2018 Playoffs',
        'wins' => 11
      )
    )
  ),
);

foreach ($teams as $team) {
  $totalGamesWon = 0;
  foreach ($team['seasons'] as $season) {
    $totalGamesWon += $season['wins'];
  }
  $team['totalGamesWon'] = $totalGamesWon;
}

echo $teams[0]['teamName'];       // outputs "Yankees"
echo $teams[0]['totalGamesWon'];  // should output "102". Instead, I get "Notice: Undefined index: totalGamesWon"

【问题讨论】:

  • 这是问题吗? foreach ($teams as &$team) {

标签: php mysql arrays


【解决方案1】:

只需将$team 更改为&$team,这样您就可以通过引用而不是按值传递$team

foreach ($teams as &$team) {
   $totalGamesWon = 0;
   foreach ($team['seasons'] as $season) {
       $totalGamesWon += $season['wins'];
   }
   $team['totalGamesWon'] = $totalGamesWon;
}

默认情况下($team)它是按值传递的,这意味着它只在范围内(在循环中)改变。因此每次迭代都会设置 $team['totalGameswon'] 的值,但实际数组 $teams 不受影响。

当它通过引用传递时,当前更改的项目 ($team['totalGamesWon']) 正在更改实际数组中的值。


所以: (按值传递)

foreach ($teams as $key => $team) {
   $totalGamesWon = 0;
   foreach ($team['seasons'] as $season) {
       $totalGamesWon += $season['wins'];
   }
   $teams[$key]['totalGamesWon'] = $totalGamesWon;
}

通过引用传递

foreach ($teams as &$team) {
   $totalGamesWon = 0;
   foreach ($team['seasons'] as $season) {
       $totalGamesWon += $season['wins'];
   }
   $team['totalGamesWon'] = $totalGamesWon;
}

会给你同样的结果。

【讨论】:

    【解决方案2】:

    问题出在“foreach ($teams as $team)”

    foreach() 创建一个新变量 $team,其中 $teams[] 的值在每个循环中都被复制。

    这意味着 $team 是一个不同于 $teams[] 的变量。 您将 totalGamesWon 写入 $team - 在下一个循环中被覆盖。 $teams[] 未受影响。

    试试这个:

    <?php
    
    $teams = array(
        array (
          'teamName' => 'Yankees',
          'seasons' => array (
            array(
              'seasonName' => '2018 Regular Season',
              'wins' => 100
            ),
            array(
              'seasonName' => '2018 Playoffs',
              'wins' => 2
            )
          )
        ),
        array (
          'teamName' => 'Red Sox',
          'seasons' => array (
            array(
              'seasonName' => '2018 Regular Season',
              'wins' => 108
            ),
            array(
              'seasonName' => '2018 Playoffs',
              'wins' => 11
            )
          )
        ),
      );
    
      $teamsCount = count($teams) ;
    
      for ($i = 0; $i < $teamsCount; $i++ ) 
      {
        $totalGamesWon = 0;
        foreach ($teams[$i]['seasons'] as $season) {
          $totalGamesWon += $season['wins'];
        }
        $teams[$i]['totalGamesWon'] = $totalGamesWon;
      }
    
      echo $teams[0]['teamName'];       // outputs "Yankees"
      echo $teams[0]['totalGamesWon'];  // should output "102". Instead, I get "Notice: Undefined index: totalGamesWon"
    
    
    die() ;
    ?>
    

    【讨论】:

    • 没有其他类型的 for 循环。您仍然可以使用包含密钥的 foreach-loop。
    猜你喜欢
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2019-02-17
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    相关资源
    最近更新 更多