我有很多棒球队,每个棒球队里都有一系列赛季。每个赛季内的获胜次数。我想统计一个团队的所有胜利,然后将此数字推入每个团队的数据中。我的最终目标是按照每个团队的总获胜次数来排序此主阵列。
我的问题是,我不知道如何将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"