如何在PHP中对多维数组中的相同键值进行求和并组合其他值?

时间:2017-02-03 08:50:44

标签: php multidimensional-array sum

我有一个多维数组。其中包含sales和rdsales值。我需要创建一个新数组,它是这两个数组的总和。

enter image description here

销售键3的值将增加rdsales键3的值,键4也相同。 保持钥匙1现在。

Final Array Like

[sum_rdsales_sales] => Array
        (
            [brand] => Array
                (
                    [1] => Array
                        (
                            [total_qty] => 3.000
                            [total_amt] => 91500.00
                        )

                    [3] => Array
                        (
                            [total_qty] => 12.020
                            [total_amt] => 336600.00
                        )

                    [4] => Array
                        (
                            [total_qty] => 6.240
                            [total_amt] => 124800.00
                        )

                )

            [sales_value] => 552900
        )

2 个答案:

答案 0 :(得分:1)

使用以下方法(使用的函数:array_sumarray_column):

$arr = [
    'sales' => ['brand'=> [
        3 => ['total_qty'=> 12.000, 'total_amt'=> 336000.00],
        4 => ['total_qty'=> 1.240, 'total_amt'=> 24800.00],
        ], 'sales_value' => 360800],
    'rdsales' => ['brand'=> [
        1 => ['total_qty'=> 3.000, 'total_amt'=> 91500.00],
        3 => ['total_qty'=> 0.020, 'total_amt'=> 600.000],
        4 => ['total_qty'=> 5.000, 'total_amt'=> 100000.00],
        ], 'sales_value' => 192100],    
];

$result = [];
$result['sum_rdsales_sales'] = [
    'brand' => [],
    'sales_value' => array_sum(array_column($arr, 'sales_value'))];

// `$prevalent_arr` is the array which has larger amount of elements
if ((count($arr['sales']['brand']) > count($arr['rdsales']['brand']))) {
    $prevalent_arr = $arr['sales']['brand'];
    $compared_arr = $arr['rdsales']['brand'];
} else {
    $prevalent_arr = $arr['rdsales']['brand'];
    $compared_arr = $arr['sales']['brand'];
}

foreach ($prevalent_arr as $k => $v) {
    if (isset($compared_arr[$k])) {        
        $result['sum_rdsales_sales']['brand'][$k]['total_qty'] = $prevalent_arr[$k]['total_qty'] + $compared_arr[$k]['total_qty'];
        $result['sum_rdsales_sales']['brand'][$k]['total_amt'] = $prevalent_arr[$k]['total_amt'] + $compared_arr[$k]['total_amt'];
    } else {
        $result['sum_rdsales_sales']['brand'][$k] = $prevalent_arr[$k];
    }
}

print_r($result);

输出:

Array
(
    [sum_rdsales_sales] => Array
        (
            [brand] => Array
                (
                    [1] => Array
                        (
                            [total_qty] => 3
                            [total_amt] => 91500
                        )

                    [3] => Array
                        (
                            [total_qty] => 12.02
                            [total_amt] => 336600
                        )

                    [4] => Array
                        (
                            [total_qty] => 6.24
                            [total_amt] => 124800
                        )

                )

            [sales_value] => 552900
        )
)

答案 1 :(得分:0)

在我的情况下,每个人都面临着不同的不同情况。 工作代码是

foreach($depots as $depot){
   $depotid = $depot['Depot']['id'];
   if(isset($reportData[$depotid])){
        $sales_value = 0;
    // brand merging and sum of total amount for sale and rdsales
    foreach($brands as $key=>$brand){
     if(isset($reportData[$depotid]['sales']['brand'][$key]) && isset($reportData[$depotid]['rdsales']['brand'][$key])){
      if(array_key_exists($key, $reportData[$depotid]['sales']['brand']) && array_key_exists($key, $reportData[$depotid]['rdsales']['brand'])){
       $reportData[$depotid]['sale_redis_add']['brand'][$key]['total_qty'] = $reportData[$depotid]['sales']['brand'][$key]['total_qty']+ $reportData[$depotid]['rdsales']['brand'][$key]['total_qty']; 
       $reportData[$depotid]['sale_redis_add']['brand'][$key]['total_amt'] = $reportData[$depotid]['sales']['brand'][$key]['total_amt']+ $reportData[$depotid]['rdsales']['brand'][$key]['total_amt'];
       $sales_value += $reportData[$depotid]['sale_redis_add']['brand'][$key]['total_amt'];
      }
     }
     else{
      if(isset($reportData[$depotid]['sales']['brand'][$key])){
       if(array_key_exists($key, $reportData[$depotid]['sales']['brand'])){
        $reportData[$depotid]['sale_redis_add']['brand'][$key]['total_qty'] = $reportData[$depotid]['sales']['brand'][$key]['total_qty'] ; 
        $reportData[$depotid]['sale_redis_add']['brand'][$key]['total_amt'] = $reportData[$depotid]['sales']['brand'][$key]['total_amt'];
        $sales_value += $reportData[$depotid]['sale_redis_add']['brand'][$key]['total_amt'];
       } 
      }
      if(isset($reportData[$depotid]['rdsales']['brand'][$key])){
       if(array_key_exists($key, $reportData[$depotid]['rdsales']['brand'])){
        $reportData[$depotid]['sale_redis_add']['brand'][$key]['total_qty'] = $reportData[$depotid]['rdsales']['brand'][$key]['total_qty'] ; 
        $reportData[$depotid]['sale_redis_add']['brand'][$key]['total_amt'] = $reportData[$depotid]['rdsales']['brand'][$key]['total_amt'];
        $sales_value += $reportData[$depotid]['sale_redis_add']['brand'][$key]['total_amt'];
       }
      }
     }
    }
    // total sale and rdsales value for a depot
    $reportData[$depotid]['sale_redis_add']['sales_value']=$sales_value; 
   }
  }