对多维数组中每个键的值求和



我有一个干草堆,我想分别求和每个数组的值。这是我的:

Array //ALL THE HAYSTACK
(
    [0] => Array //REPRESENTS MONTH 1
        (
            [0] => Array  //REPRESENTS WEEK 1
                (
                    [week] => 1
                    [user1] => Array
                        (
                            [0] => Array
                                (
                                    [sell] => value
                                    [cost] => value
                                    [gain] => value
                                )
                        )
                    [user2] => Array
                        (
                            [0] => Array
                                (
                                    [sell] => value
                                    [cost] => value
                                    [gain] => value
                                )
                        )
                    [user3] => Array
                        (
                            [0] => Array
                                (
                                    [sell] => value
                                    [cost] => value
                                    [gain] => value
                                )
                        )
                )
            //... MORE WEEKS
        [1] => Array //REPRESENTS MONTH 2
        ( //... ALL THE PROCCES ABOVE
) 

我试着每个月和每周使用foreach(),像这样:

$result = $array('sell' => 0, 'cost' => 0, 'gain' => 0);
foreach ($monthArray as $theMonth => $week){
    foreach ($week as $user){
        $result['sell'] +=  $user['user1'][0]['sell'];
        $result['sell'] +=  $user['user2'][0]['sell'];
        $result['sell'] +=  $user['user3'][0]['sell'];
        //...

但是,当我打印$result他SUM EVERY MONTHS,而不是我想要的特定月份…

我每周和每月都要sum。如何解决这个问题?

这个怎么样?:结果($sales)为array。这个数组的键是月份,数组的值是总数。通过迭代每个月的周数,然后每周迭代用户来获得总数。后面的迭代我介绍了一下,因为您使用的是这个硬编码的user1、user2、user3列表....

与其将其存储在另一个数组中,还可以立即回显或使用它,但我认为将其存储在数组中更便于进一步处理。

$result = $array('sell' => 0, 'cost' => 0, 'gain' => 0);
foreach ($monthArray as $theMonth => $week){
    $sales[$theMonth] = 0;
    foreach ($week as $users){
        foreach ($users as $user) {
            $sales[$theMonth] +=  $user[0]['sell'];
        }
    }
}
result['sell'] = $sales;
print_r(result['sell']);

如果您也希望每个月都有成本和收益,那么创建一个格式为$result[month]['sell']而不是$result['sell'][month]的数组可能更有意义。

您可以尝试使用array_reduce()array_column()。示例:这里:

//$array = your array
$sum_arr = array_reduce($array, function($old, $new){
    $arr = array_filter(array_map(function($v){ return is_array($v) ? $v : '';}, array_shift($new)));
    return [
        'sell' => array_sum(array_column($arr, 'sell')) + $old['sell'],
        'cost' => array_sum(array_column($arr, 'cost')) + $old['cost'],
        'gain' => array_sum(array_column($arr, 'gain')) + $old['gain'],
    ];
}, ['sell'=>0, 'cost'=>0, 'gain'=>0]);

更新:

array_column()的替代:

$sum_arr = array_reduce($array, function($old, $new){
    $arr = array_filter(array_map(function($v){ return is_array($v) ? $v : '';}, array_shift($new)));
    return [
        'sell' => array_sum(array_map(function($v){return $v['sell'];}, $arr)) + $old['sell'],
        'cost' => array_sum(array_map(function($v){return $v['cost'];}, $arr)) + $old['cost'],
        'gain' => array_sum(array_map(function($v){return $v['gain'];}, $arr)) + $old['gain'],
    ];
}, ['sell'=>0, 'cost'=>0, 'gain'=>0]);

相关内容

  • 没有找到相关文章

最新更新