在 3d 数组上循环时,通过将先前的总计和当前的“计数”值相加来更新每个“总计”值

问题描述 投票:0回答:3

我有一个数组:

[
    '2015-09-23' => [
        'user' => [13, 12],
        'count' => [
            'count' => 2,
            'total' => 2,
        ],
    ],
    '2015-09-24' => [
        'user' => [14],
        'count' => [
            'count' => 1,
            'total' => 1,
        ],
    ],
    '2015-09-25' => [
        'user' => [15],
        'count' => [
            'count' => 1,
            'total' => 1,
        ],
    ],
    '2015-09-26' => [
        'user' => [16],
        'count' => [
            'count' => 1,
            'total' => 1,
        ],
    ],
    '2015-09-27' => [
        'user' => [17, 18],
        'count' => [
            'count' => 2,
            'total' => 2,
        ],
    ],
    '2015-09-28' => [
        'user' => [19, 20, 21, 22, 23, 24, 25, 26, 27, 28],
        'count' => [
            'count' => 10,
            'total' => 10,
        ],
    ],
]

我的愿景是通过前一个项目的总计值加上当前项目的[“计数”]来增加每个项目的[“总计”]。例如,如果前一个值为 2,当前值为 2,则总数将为 4。如果下一个值为 1,则将为 5。

这是我最新的尝试:

$arr_keys = array_keys($this->tmp_data['month_users_formatted']);
foreach ( array_keys($arr_keys) as $key ) {
    $this_value = $this->tmp_data['month_users_formatted'][$arr_keys[$key]];
    if ( isset($this->tmp_data['month_users_formatted'][$arr_keys[$key - 1]]) ){
        $prev_value = $this->tmp_data['month_users_formatted'][$arr_keys[$key - 1]];
        $this_value['count']['total'] = $this_value['count']['total'] + $prev_value['count']['total'];
    }
}

想要的结果:

Array
(
    [2015-09-23] => Array
        (
            [user] => Array
                (
                    [0] => 13
                    [1] => 12
                )

            [count] => Array
                (
                    [count] => 2
                    [total] => 2
                )

        )

    [2015-09-24] => Array
        (
            [user] => Array
                (
                    [0] => 14
                )

            [count] => Array
                (
                    [count] => 1
                    [total] => 3
                )

        )

    [2015-09-25] => Array
        (
            [user] => Array
                (
                    [0] => 15
                )

            [count] => Array
                (
                    [count] => 1
                    [total] => 4
                )

        )

    [2015-09-26] => Array
        (
            [user] => Array
                (
                    [0] => 16
                )

            [count] => Array
                (
                    [count] => 1
                    [total] => 5
                )

        )

    [2015-09-27] => Array
        (
            [user] => Array
                (
                    [0] => 17
                    [1] => 18
                )

            [count] => Array
                (
                    [count] => 2
                    [total] => 7
                )

        )

    [2015-09-28] => Array
        (
            [user] => Array
                (
                    [0] => 19
                    [1] => 20
                    [2] => 21
                    [3] => 22
                    [4] => 23
                    [5] => 24
                    [6] => 25
                    [7] => 26
                    [8] => 27
                    [9] => 28
                )

            [count] => Array
                (
                    [count] => 10
                    [total] => 17
                )

        )

)
php arrays loops multidimensional-array cumulative-sum
3个回答
1
投票
    $total = 0;
    foreach($countsAndTotals as $key => $countAndTotal) {
        $total += $countAndTotal['count']['count'];
        $countsAndTotals[$key]['count']['total'] = $total;
    }

    print_r($countsAndTotals);

这将获取数组并将所有计数值相加,然后用 $total 变量替换总值。


0
投票

您可以使用 foreach 并通过引用设置 var :

$muf = $this->tmp_data['month_users_formatted'];
$total = 0;
foreach($muf as &$month){
  $month['count']['total']+=$total;
  $total+= $month['count']['total'];
}

0
投票

函数式方法可以将前一个迭代的总数保留为

static
变量,以便下一次迭代可以访问它。

代码:(演示)(精简作业

var_export(
    array_map(
        function ($set) {
            static $prev = 0;
            $set['count']['total'] += $prev;
            $prev = $set['count']['total'];
            return $set;
        },
        $array
    )
);

或者您可以使用数组解构来访问计数值并通过引用修改总计值。

代码:(演示)(非压缩作业

$total = 0;
foreach ($array as ['count' => ['count' => $c, 'total' => &$t]]) {
    $t = $total += $c;
} 
var_export($array);
© www.soinside.com 2019 - 2024. All rights reserved.