1

I need group rows of data by a column and sum another column.

Input:

[
    ['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'],
];

Desired result:

[
    ['quantity' => 15, 'dd' => '01-Nov-2012'],
    ['quantity' => 3,  'dd' => '02-Nov-2012'],
    ['quantity' => 19, 'dd' => '03-Nov-2012'],
];
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
user15341
  • 27
  • 1
  • 2

3 Answers3

2

Just loop trough the rows, and add up the quantities in a different array indexed by the dd values.

$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);
complex857
  • 20,425
  • 6
  • 51
  • 54
1

Orrrrr... Since the days are unique:

  <?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);

would produce:

Array
(
    [01-Nov-2012] => 15
    [02-Nov-2012] => 3
    [03-Nov-2012] => 19
)
Ariaan
  • 1,105
  • 1
  • 9
  • 13
  • This advice belongs on: [Group array data on one column and sum data from another column to form a flat associative array](https://stackoverflow.com/q/3286749/2943403) asked in 2010. – mickmackusa Apr 16 '23 at 05:28
0

There are a number of viable techniques to group and sum your 2d array data.

  1. foreach() with temporary keys: (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));
    

  2. foreach() with references instead of temporary result keys: (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);
    

  3. array_reduce() with temporary keys: (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;
                }
            )
        )
     );
    

  4. array_reduce() with references instead of temporary keys: (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;
            }
        )
    );
    

To execute more summing actions with other column values, duplicate the summing action found in the else branch of any of these snippets.

For example:

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

To group by more than one column, create a "composite key" by concatentating multiple column values into a single, temporary string key. Here are some demonstrations of the technique:

mickmackusa
  • 43,625
  • 12
  • 83
  • 136