查找重复的值,将值分组,然后对PHP中的值求和

时间:2019-07-16 16:35:43

标签: php arrays multidimensional-array

我有这个数组,我尝试对valorDolar进行分组和求和,但是我不能

所以,在我的代码中,我在数组中运行数组以查找相等的数据并将其分组。

我的代码

const { PDFRStreamForBuffer, createWriterToModify, PDFStreamForResponse } = require('hummus');
const { WritableStream } = require('memory-streams');

// Merge the pages of the pdfBlobs (Javascript buffers) into a single PDF blob                                                                                                                                                                  
const mergePdfs = pdfBlobs => {
  if (pdfBlobs.length === 0) throw new Error('mergePdfs called with empty list of PDF blobs');
  // This optimization is not necessary, but it avoids the churn down below                                                                                                                                                
  if (pdfBlobs.length === 1) return pdfBlobs[0];

  // Adapted from: https://stackoverflow.com/questions/36766234/nodejs-merge-two-pdf-files-into-one-using-the-buffer-obtained-by-reading-them?answertab=active#tab-top                                                     
  // Hummus is useful, but with poor interfaces -- E.g. createWriterToModify shouldn't require any PDF stream                                                                                                              
  // And Hummus has many Issues: https://github.com/galkahana/HummusJS/issues                                                                                                                                              
  const [firstPdfRStream, ...restPdfRStreams] = pdfBlobs.map(pdfBlob => new PDFRStreamForBuffer(pdfBlob));
  const outStream = new WritableStream();
  const pdfWriter = createWriterToModify(firstPdfRStream, new PDFStreamForResponse(outStream));
  restPdfRStreams.forEach(pdfRStream => pdfWriter.appendPDFPagesFromPDF(pdfRStream));
  pdfWriter.end();
  outStream.end();
  return outStream.toBuffer();
};

module.exports = exports = {
  mergePdfs,
};

该代码运行到同一数组中以查找重复的Fraccion,并组成Fraccion的组,并对找到的所有valorDolar求和。

    foreach ($array_DocumentOne as $key => $value) {
        foreach ($array_DocumentOne as $k => $v) {
            if ($key < $k && $value['fraccion'] == $v['fraccion']) {

                $sum = $value['valorDolar'] + $v['valorDolar'];

                $duplicateInDocOne[] = array(
                    $value,
                    $v,
                    $sum
                );
            }
        }
    }

我想看到这样的数组

[data] => Array
            (
                [0] => Array
                    (
                  [0] => Array
                    (
                        [0] => Array
                            (
                                [sec] => 2
                                [fraccion] => 39232101
                                [SUBD] => 
                                [vinc] => 0
                                [met_val] => 6
                                [umc] => 6
                                [cantidad_umc] => 12000
                                [umt] => 1
                                [cantidad_umt] => 16.32
                                [pvc] => MEX
                                [pod] => MEX
                                [Descripcion] => "text"
                                [valorDolar] => 175.20
                            )

                        [1] => Array
                            (
                                [sec] => 5
                                [fraccion] => 39232101
                                [SUBD] => 
                                [vinc] => 0
                                [met_val] => 6
                                [umc] => 6
                                [cantidad_umc] => 5000
                                [umt] => 1
                                [cantidad_umt] => 13.125
                                [pvc] => MEX
                                [pod] => MEX
                                [Descripcion] => "tex"
                                [valorDolar] => 388.10
                            )

                        [2] => 563.3
                    )

                [1] => Array
                    (
                        [0] => Array
                            (
                                [sec] => 2
                                [fraccion] => 39232101
                                [SUBD] => 
                                [vinc] => 0
                                [met_val] => 6
                                [umc] => 6
                                [cantidad_umc] => 12000
                                [umt] => 1
                                [cantidad_umt] => 16.32
                                [pvc] => MEX
                                [pod] => MEX
                                [Descripcion] => "text"
                                [valorDolar] => 175.20
                            )

                        [1] => Array
                            (
                                [sec] => 6
                                [fraccion] => 39232101
                                [SUBD] => 
                                [vinc] => 0
                                [met_val] => 6
                                [umc] => 6
                                [cantidad_umc] => 2000
                                [umt] => 1
                                [cantidad_umt] => 0.62
                                [pvc] => MEX
                                [pod] => MEX
                                [Descripcion] => "text"
                                [valorDolar] => 169.08
                            )

                        [2] => 344.28
                    )

1 个答案:

答案 0 :(得分:0)

我不确定这是您要寻找的东西,但是我尝试一下...

我输入了一些数据进行测试:

    Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [sec] => 2
                                [valorDolar] => 100
                                [fraccion] => 15453
                            )

                        [1] => Array
                            (
                                [sec] => 2
                                [valorDolar] => 200
                                [fraccion] => 16453
                            )

                        [2] => Array
                            (
                                [sec] => 3
                                [valorDolar] => 400
                                [fraccion] => 15453
                            )

                        [3] => Array
                            (
                                [sec] => 3
                                [valorDolar] => 600
                                [fraccion] => 16453
                            )

                    )

            )

    )

If I understand you correctly you would like to group by fraccion. 

您可以使用以下事实:可以将任何索引作为键存储在PHP数组中:

    $group_arr = array();
    foreach ($array_DocumentOne as $outer_key => $outer_array) {
        foreach ( $outer_array as $inner_key => $inner_array) {
            foreach ( $inner_array as $item_key => $array_item) {
                $groupby_item = $array_item['fraccion'];
                $group_arr[$groupby_item][] = $array_item;
            }
        }
     }

,您将获得一个像这样的数组($group_arr):

Array
(
    [15453] => Array
        (
            [0] => Array
                (
                    [sec] => 2
                    [valorDolar] => 100
                    [fraccion] => 15453
                )

            [1] => Array
                (
                    [sec] => 3
                    [valorDolar] => 400
                    [fraccion] => 15453
                )

        )

    [16453] => Array
        (
            [0] => Array
                (
                    [sec] => 2
                    [valorDolar] => 200
                    [fraccion] => 16453
                )

            [1] => Array
                (
                    [sec] => 3
                    [valorDolar] => 600
                    [fraccion] => 16453
                )

        )

)

...,您可以用来汇总每个组的值。