对数组项求和

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

我有一个数组:

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] => 1
                )

        )

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

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

        )

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

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

        )

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

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

        )

    [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] => 10
                )

        )

)

我的愿景是将每个值 count['count'] 增加前一个元素的值。例如,如果前一个值是 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
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.