【问题标题】:How to sum same key values and combine other values in multidimentional array in PHP?如何在 PHP 中对多维数组中的相同键值求和并组合其他值?
【发布时间】:2017-02-03 08:50:43
【问题描述】:

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

销售键 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
        )

【问题讨论】:

  • 显示最终数组的外观
  • 谢谢@RomanPerekhrest。我刚刚添加了最终数组值。

标签: php multidimensional-array sum


【解决方案1】:

使用如下方法(使用过的函数:array_sum,array_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
        )
)

【讨论】:

  • 感谢您的想法,我从这个答案中得到了帮助。根据我目前的问题,您的答案是正确的,但我遇到一种情况,“sales”和“rdsales”具有不同的键,例如“sales”具有一个键为“1”,而“rdsales”具有一个键为“3”。在这种情况下,解决方案将不起作用。但是感谢您的重播。我有了一些想法并解决了我的问题。再次感谢:)
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多