普通网友 2018-02-12 06:37
浏览 119
已采纳

如何使用两列中的值对子数组进行分组,然后对第三列的值求和?

This is a section of my array:

[1] => Array
(
    [quantity] => 2
    [product_id] => 1
    [option_id] => 22
)

[2] => Array
(
    [quantity] => 2
    [product_id] => 2
    [option_id] => 22
)

[3] => Array
(
    [quantity] => 3
    [product_id] => 2
    [option_id] => 22
)

[4] => Array
(
    [quantity] => 1
    [product_id] => 2
    [option_id] => 25
)

I wish to group/merge the subarrays by product_id and option_id.

Upon merging subarrays, I would like to sum the quantity values.

In my sample data, both subarrays [2] and [3] have 'product_id'=>2 and 'option_id'=>22. They should be merged together into one subarray with a quantity value of 5.

This is my expected output:

[1] => Array
(
    [quantity] => 2
    [product_id] => 1
    [option_id] => 22
)

[2] => Array
(
    [quantity] => 5
    [product_id] => 2
    [option_id] => 22
)

[3] => Array
(
    [quantity] => 1
    [product_id] => 2
    [option_id] => 25
)

*My first level keys are not associated with their subarrays so they may be changed in the process. I do want the first level keys to be incremented from 1 not 0.

  • 写回答

2条回答 默认 最新

  • douchenhui5569 2018-02-12 08:03
    关注

    You only need to build compound temporary keys and then overwrite the keys.

    By writing an initial element (null in this case) then deleting the element after the loop is finished, you ensure that the first key in the result array is 1.

    To avoid the "messiness" of the repeated long-winded compound key, you can save the the dynamic key as a variable in the first line inside of the foreach loop, then reference that variable in the three respective locations in the condition block.

    Code (Demo)

    $array = [
        1 => ['quantity' => 2, 'product_id' => 1, 'option_id' => 22],
        2 => ['quantity' => 2, 'product_id' => 2, 'option_id' => 22],
        3 => ['quantity' => 3, 'product_id' => 2, 'option_id' => 22],
        4 => ['quantity' => 1, 'product_id' => 2, 'option_id' => 25]
    ];
    
    $result = [null];  // create placeholding element
    foreach($array as $subarray){
        $composite_key = $subarray['product_id'] . '_' . $subarray['option_id'];
        if(!isset($result[$composite_key])){
            $result[$composite_key] = $subarray;  // first occurrence
        }else{
            $result[$composite_key]['quantity'] += $subarray['quantity'];  // not first occurrence
        }
    }
    $result=array_values($result);  // change from assoc to indexed
    unset($result[0]);  // remove first element to start numeric keys at 1
    var_export($result);
    

    Output:

    array (
      1 => 
      array (
        'quantity' => 2,
        'product_id' => 1,
        'option_id' => 22,
      ),
      2 => 
      array (
        'quantity' => 5,
        'product_id' => 2,
        'option_id' => 22,
      ),
      3 => 
      array (
        'quantity' => 1,
        'product_id' => 2,
        'option_id' => 25,
      ),
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB四叉树处理长方形tif文件
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B