php 按一列对2d数组的行数据进行分组,并对每组中的另一列求和,以生成简化的2d数组

nfzehxib  于 2023-04-19  发布在  PHP
关注(0)|答案(3)|浏览(131)

我需要一组数据行的一列和另一列的总和。
输入:

[
    ['quantity' => 5,  'dd' => '01-Nov-2012'],
    ['quantity' => 10, 'dd' => '01-Nov-2012'],
    ['quantity' => 3,  'dd' => '02-Nov-2012'],
    ['quantity' => 4,  'dd' => '03-Nov-2012'],
    ['quantity' => 15, 'dd' => '03-Nov-2012'],
];

预期结果:

[
    ['quantity' => 15, 'dd' => '01-Nov-2012'],
    ['quantity' => 3,  'dd' => '02-Nov-2012'],
    ['quantity' => 19, 'dd' => '03-Nov-2012'],
];
vuktfyat

vuktfyat1#

只需循环遍历这些行,并将数量加到由dd值索引的不同数组中。

$in = array(array()); // your input
$out = array();
foreach ($in as $row) {
    if (!isset($out[$row['dd']])) {
        $out[$row['dd']] = array(
            'dd' => $row['dd'],
            'quantity' => 0,
        );
    }
    $out[$row['dd']]['quantity'] += $row['quantity'];
}
$out = array_values($out); // make the out array numerically indexed
var_dump($out);
m528fe3b

m528fe3b2#

因为日子是独一无二的:

<?php

  header('Content-type: text/plain');

  $inputArray  = array(
    array('qty' => 5,  'dd'  => '01-Nov-2012'),
    array('qty' => 10, 'dd'  => '01-Nov-2012'),
    array('qty' => 3,  'dd'  => '02-Nov-2012'),
    array('qty' => 4,  'dd'  => '03-Nov-2012'),
    array('qty' => 15, 'dd'  => '03-Nov-2012'));

  $outputArray = array();

  foreach ( $inputArray as $record ) {
    if ( !key_exists($record['dd'], $outputArray) ) {
      $outputArray[$record['dd']] = 0;
    }
    $outputArray[$record['dd']] += $record['qty'];
  }

  print_r($outputArray);

将产生:

Array
(
    [01-Nov-2012] => 15
    [02-Nov-2012] => 3
    [03-Nov-2012] => 19
)
bjp0bcyl

bjp0bcyl3#

有许多可行的技术可以对2d数组数据进行分组和求和。

  1. foreach()和临时密钥:(Demo
$result = [];
foreach ($array as $row) {
    if (!isset($result[$row['dd']])) {
        $result[$row['dd']] = $row;
    } else {
        $result[$row['dd']]['quantity'] += $row['quantity'];
    }
}
var_export(array_values($result));
  1. foreach()使用引用而不是临时结果键:(Demo
$result = [];
foreach ($array as $row) {
    if (!isset($ref[$row['dd']])) {
        $ref[$row['dd']] = $row;
        $result[] = &$ref[$row['dd']];
    } else {
        $ref[$row['dd']]['quantity'] += $row['quantity'];
    }
}
var_export($result);
  1. array_reduce()和临时密钥:(Demo
var_export(
    array_values(
        array_reduce(
            $array,
            function($result, $row) {
                if (!isset($result[$row['dd']])) {
                    $result[$row['dd']] = $row;
                } else {
                    $result[$row['dd']]['quantity'] += $row['quantity'];
                }
                return $result;
            }
        )
    )
 );
  1. array_reduce()使用引用而不是临时键:(Demo
var_export(
    array_reduce(
        $array,
        function($result, $row) {
            static $ref = [];
            if (!isset($ref[$row['dd']])) {
                $ref[$row['dd']] = $row;
                $result[] = &$ref[$row['dd']];
            } else {
                $ref[$row['dd']]['quantity'] += $row['quantity'];
            }
            return $result;
        }
    )
);

要对其他列值执行更多求和操作,请复制这些代码段的else分支中的求和操作。
例如:

} else {
    $result[$row['dd']]['quantity'] += $row['quantity'];
    $result[$row['dd']]['anotherColumn'] += $row['anotherColumn'];
    // ... and so on
}

要按多个列进行分组,请通过将多个列值连接到单个临时字符串键中来创建“复合键”。以下是该技术的一些演示:

相关问题