对多维数组的深层列值求和并将结果映射到同一数组中的另一个元素

发布于 2024-10-08 10:30:05 字数 1506 浏览 0 评论 0原文

我正在试验 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以为你会在 2024-10-15 10:30:05

试试这个:

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];
                }
        }
}

查看

Try this:

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];
                }
        }
}

See it

葬﹪忆之殇 2024-10-15 10:30:05

这应该可以完成您想要完成的任务,但尚未测试过。

foreach ($t1['matches'] as $key=>$value){
   $wins = 0;
   $losses = 0;
   $draws = 0;
   foreach($value as $record){
      $wins   += $record[0];
      $losses += $record[1];
      $draws  += $record[2];
   }

   $t1['overallSeasons'][$key] = array($wins, $losses, $draws);
}

This should do what you're trying to accomplish, havn't tested it though.

foreach ($t1['matches'] as $key=>$value){
   $wins = 0;
   $losses = 0;
   $draws = 0;
   foreach($value as $record){
      $wins   += $record[0];
      $losses += $record[1];
      $draws  += $record[2];
   }

   $t1['overallSeasons'][$key] = array($wins, $losses, $draws);
}
身边 2024-10-15 10:30:05

循环匹配子数组以访问季节值。然后对与父循环季节值相关的每列数据进行 array_sum() 的映射调用。这足够灵活,可以处理深行中除 3 列以外的情况。

代码:(演示)

foreach ($t1['matches'] as $season => $weeks) {
    $t1['overallSeasons'][$season] = array_map(
        fn(...$col) => array_sum($col),
        ...array_values($t1['matches'][$season])
    );
}
var_export($t1);

如果这种技术太难遵循,那么相当于 2 个经典循环。内部循环可以安全地对周数据使用数组解构,因为只会遇到 3 元素行。

代码:(演示)

foreach ($t1['matches'] as $season => $weeks) {
    foreach ($weeks as [$w, $l, $d]) {
        $t1['overallSeasons'][$season][0] += $w;
        $t1['overallSeasons'][$season][1] += $l;
        $t1['overallSeasons'][$season][2] += $d;
    }
}
var_export($t1);

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)

foreach ($t1['matches'] as $season => $weeks) {
    $t1['overallSeasons'][$season] = array_map(
        fn(...$col) => array_sum($col),
        ...array_values($t1['matches'][$season])
    );
}
var_export($t1);

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)

foreach ($t1['matches'] as $season => $weeks) {
    foreach ($weeks as [$w, $l, $d]) {
        $t1['overallSeasons'][$season][0] += $w;
        $t1['overallSeasons'][$season][1] += $l;
        $t1['overallSeasons'][$season][2] += $d;
    }
}
var_export($t1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文