从子数组(PHP)中将数据推送到父数组

时间:2020-05-17 19:19:36

标签: php mysql arrays

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

我的问题是,我不知道如何将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"

2 个答案:

答案 0 :(得分:0)

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

foreach()创建一个新变量$ team,其中每个周期都会复制$ teams []的值。

这意味着$ team是与$ teams []不同的变量。 您在$ team中编写了totalGamesWon-在下一个循环中将被覆盖。 $ 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() ;
?>

答案 1 :(得分:0)

只需将$team更改为&$team,就可以通过{strong>引用(而不是按值)传递$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;
}

会给您相同的结果。

相关问题