普通网友 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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示