如何使用两列中的值对子数组进行分组,然后对第三列中的值求和? [英] How to group subarrays using values from two columns then sum a third column's values?

查看:119
本文介绍了如何使用两列中的值对子数组进行分组,然后对第三列中的值求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我数组的一部分:

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

这篇关于如何使用两列中的值对子数组进行分组,然后对第三列中的值求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆