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

How to group subarrays using values from two columns then sum a third column#39;s values?(如何使用两列中的值对子数组进行分组,然后对第三列的值求和?)
本文介绍了如何使用两列中的值对子数组进行分组,然后对第三列的值求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数组的一部分:

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)