对多维数组的深层列值求和并将结果映射到同一数组中的另一个元素
我正在试验 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")
)
)
);
我想要实现的目标是将每个赛季所有周的所有胜利、失败和平局添加到各自的赛季总数中。
例如,$t1["matches"]["season1.cs"]
中所有周的总和将添加到 $t1["overallSeasons"]["season1.cs “]
。结果是:
"overallSeasons" => array (
"season1.cs" => array (28, 19, 1),
"season2.cs" => array (21, 4, 7)
),
在过去的一个小时里,我试图自己解决这个问题,而我所得到的只是对 for 循环 和 foreach-loops 有了更多的了解。 。所以我想我现在已经掌握了基础知识,例如使用 foreach 循环等。然而,我对此还很陌生,所以请耐心等待!
我可以让循环到达 $t1["matches"]
键并遍历每个赛季,但我似乎不知道如何添加所有胜利每一周的、损失和平局。
目前,我只是寻找有关整个赛季总和的答案,因为一旦我弄清楚如何实现这一目标,我就可以从那里开始工作。
I'm experimenting with arrays in PHP and I am setting up a fake environment where a "team's" record is held in arrays.
$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")
)
)
);
What I am trying to achieve is to add all the wins, loss, and draws from all weeks of each season to their respective season total.
For example, the sum of all the weeks in $t1["matches"]["season1.cs"]
will be added to $t1["overallSeasons"]["season1.cs"]
. The result would leave:
"overallSeasons" => array (
"season1.cs" => array (28, 19, 1),
"season2.cs" => array (21, 4, 7)
),
I tried to work this out on my own for the past hour and all I have gotten is a little more knowledge of for-loops and foreach-loops . So I think I now have the basics down such as using foreach loops and so on. However, I am still fairly new to this so bear with me!
I can get the loop to the point of the $t1["matches"]
key and go through each season, but I can't seem to figure out how to add all of the wins, loss, and draw, for each individual week.
For now, I'm only looking for answers concerning the overall seasons sum since I can work from there once I figure out how to achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
查看
Try this:
See it
这应该可以完成您想要完成的任务,但尚未测试过。
This should do what you're trying to accomplish, havn't tested it though.
循环匹配子数组以访问季节值。然后对与父循环季节值相关的每列数据进行
array_sum()
的映射调用。这足够灵活,可以处理深行中除 3 列以外的情况。代码:(演示)
如果这种技术太难遵循,那么相当于 2 个经典循环。内部循环可以安全地对周数据使用数组解构,因为只会遇到 3 元素行。
代码:(演示)
Loop over the matches subarray to access the season values. Then make mapped calls of
array_sum()
on each column of data relating to the parent loop's season value. This is flexible enough to handle other than 3 columns in the deep rows.Code: (Demo)
If this technique is too hard to follow, then the equivalent would be 2 classic loops. The inner loop can safely use array destructuring on the week data because only 3-element rows will ever be encountered.
Code: (Demo)