lem*_*ole 3 php arrays foreach for-loop multidimensional-array
我在PHP中试验数组,我正在设置一个假的环境,其中"团队"记录保存在数组中.
$t1 = array (
"basicInfo" => array (
"The Sineps",
"December 25, 2010",
"lemonpole"
),
"overallRecord" => array (
0,
0,
0,
0
),
"overallSeasons" => array (
"season1.cs" => array (0, 0, 0),
"season2.cs" => array (0, 0, 0)
),
"matches" => array (
"season1.cs" => array (
"week1" => array ("12", "3", "1"),
"week2" => array ("8", "8" ,"0"),
"week3" => array ("8", "8" ,"0")
),
"season2.cs" => array (
"week1" => array ("9", "2", "5"),
"week2" => array ("12", "2" ,"2")
)
)
);
Run Code Online (Sandbox Code Playgroud)
我想要实现的是将所有的胜利,损失和抽奖从每个赛季的一周添加到他们各自的一周.因此,例如,$ t1 ["匹配"] ["season1.cs"]中所有周的总和将被添加到$ t1 ["overallSeasons"] ["season1.cs"].结果将留下:
"overallSeasons" => array (
"season1.cs" => array (28, 19, 1),
"season2.cs" => array (21, 4, 7)
),
Run Code Online (Sandbox Code Playgroud)
我试着在过去一小时内自己解决这个问题,而且我得到的是对for循环和foreach循环的更多了解:o ...所以我认为我现在已经掌握了基础知识,例如使用foreach循环等等; 但是,我仍然相当新,所以忍受我!我可以让循环指向$ t1 ["匹配"]键并经历每个赛季,但我似乎无法弄清楚如何为每个星期添加所有的胜利,损失和平局.就目前而言,我只是在寻找有关整体季节总和的答案,因为一旦我弄清楚如何实现这一点,我就可以在那里工作.任何帮助将不胜感激,但请尽量为我保持简单...或者相应地评论代码!
谢谢!
试试这个:
foreach($t1['matches'] as $season => $season_array) {
foreach($season_array as $week => $week_array) {
for($i=0;$i<3;$i++) {
$t1['overallSeasons'][$season][$i] += $week_array[$i];
}
}
}
Run Code Online (Sandbox Code Playgroud)