<legend id='xUzi4'><style id='xUzi4'><dir id='xUzi4'><q id='xUzi4'></q></dir></style></legend>

<small id='xUzi4'></small><noframes id='xUzi4'>

  1. <i id='xUzi4'><tr id='xUzi4'><dt id='xUzi4'><q id='xUzi4'><span id='xUzi4'><b id='xUzi4'><form id='xUzi4'><ins id='xUzi4'></ins><ul id='xUzi4'></ul><sub id='xUzi4'></sub></form><legend id='xUzi4'></legend><bdo id='xUzi4'><pre id='xUzi4'><center id='xUzi4'></center></pre></bdo></b><th id='xUzi4'></th></span></q></dt></tr></i><div id='xUzi4'><tfoot id='xUzi4'></tfoot><dl id='xUzi4'><fieldset id='xUzi4'></fieldset></dl></div>

      <tfoot id='xUzi4'></tfoot>
        <bdo id='xUzi4'></bdo><ul id='xUzi4'></ul>

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

      How to group subarrays using values from two columns then sum a third column#39;s values?(如何使用两列中的值对子数组进行分组,然后对第三列的值求和?)
    2. <tfoot id='b5TOH'></tfoot>
            <i id='b5TOH'><tr id='b5TOH'><dt id='b5TOH'><q id='b5TOH'><span id='b5TOH'><b id='b5TOH'><form id='b5TOH'><ins id='b5TOH'></ins><ul id='b5TOH'></ul><sub id='b5TOH'></sub></form><legend id='b5TOH'></legend><bdo id='b5TOH'><pre id='b5TOH'><center id='b5TOH'></center></pre></bdo></b><th id='b5TOH'></th></span></q></dt></tr></i><div id='b5TOH'><tfoot id='b5TOH'></tfoot><dl id='b5TOH'><fieldset id='b5TOH'></fieldset></dl></div>
            <legend id='b5TOH'><style id='b5TOH'><dir id='b5TOH'><q id='b5TOH'></q></dir></style></legend>

              <tbody id='b5TOH'></tbody>

            <small id='b5TOH'></small><noframes id='b5TOH'>

              • <bdo id='b5TOH'></bdo><ul id='b5TOH'></ul>
                本文介绍了如何使用两列中的值对子数组进行分组,然后对第三列的值求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                这是我的数组的一部分:

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

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

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  <tbody id='tN6QR'></tbody>
              • <small id='tN6QR'></small><noframes id='tN6QR'>

                      • <bdo id='tN6QR'></bdo><ul id='tN6QR'></ul>
                        <legend id='tN6QR'><style id='tN6QR'><dir id='tN6QR'><q id='tN6QR'></q></dir></style></legend>
                      • <i id='tN6QR'><tr id='tN6QR'><dt id='tN6QR'><q id='tN6QR'><span id='tN6QR'><b id='tN6QR'><form id='tN6QR'><ins id='tN6QR'></ins><ul id='tN6QR'></ul><sub id='tN6QR'></sub></form><legend id='tN6QR'></legend><bdo id='tN6QR'><pre id='tN6QR'><center id='tN6QR'></center></pre></bdo></b><th id='tN6QR'></th></span></q></dt></tr></i><div id='tN6QR'><tfoot id='tN6QR'></tfoot><dl id='tN6QR'><fieldset id='tN6QR'></fieldset></dl></div>
                        <tfoot id='tN6QR'></tfoot>