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

2022-01-09 00:00:00 multidimensional-array sum grouping php

这是我的数组的一部分:

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
)

我希望通过 product_idoption_id 对子数组进行分组/合并.

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

在合并子数组时,我想对 quantity 值求和.

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

在我的示例数据中,子数组 [2][3] 都有 'product_id'=>2'option_id'=>22.它们应该合并到一个子数组中,quantity 值为 5.

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.

这是我的预期输出:

[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 而不是 0 递增第一级键.

*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.

推荐答案

你只需要构建复合临时键,然后覆盖这些键.

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

通过写入一个初始元素(本例中为 null)然后在循环结束后删除该元素,您可以确保结果数组中的第一个键是 1.

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.

为避免重复冗长的复合键的混乱",您可以将动态键作为变量保存在 foreach 循环内的第一行中,然后在条件中的三个相应位置引用该变量块.

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.

代码(演示)

$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);

输出:

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,
  ),
)

相关文章