Home  >  Article  >  Backend Development  >  How do I Group Multidimensional Array Data by Multiple Columns and Aggregate Values in PHP?

How do I Group Multidimensional Array Data by Multiple Columns and Aggregate Values in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-11-07 03:53:02259browse

How do I Group Multidimensional Array Data by Multiple Columns and Aggregate Values in PHP?

Grouping Multidimensional Array Data by Multiple Columns and Aggregating Values

Problem Statement:

You have a multidimensional array that combines data from two separate database queries. The array consists of records with attributes that include "part," "address," "type," and "count." The goal is to group the array elements based on the "part" and "type" values and calculate the sum of the "count" values within each group.

Example Input:

<code class="php">$arr1 = [
    ['part' => '1', 'address' => 'aaa', 'type' => '1', 'count' => 5],
    ['part' => '1', 'address' => 'bbb', 'type' => '1', 'count' => 5],
    ['part' => '1', 'address' => 'ccc', 'type' => '1', 'count' => 5],
    ['part' => '2', 'address' => 'aaa', 'type' => '1', 'count' => 5],
    ['part' => '2', 'address' => 'bbb', 'type' => '1', 'count' => 5],
    ['part' => '2', 'address' => 'ccc', 'type' => '2', 'count' => 5]
];</code>

Expected Output:

<code class="php">$arr2 = [
    ['part' => '1', 'type' => '1', 'count' => 15],
    ['part' => '2', 'type' => '1', 'count' => 10],
    ['part' => '2', 'type' => '2', 'count' => 5]
];</code>

Solution:

To achieve the desired grouping and aggregation, we can utilize PHP's array manipulation functions. Consider the following function:

<code class="php">function groupByPartAndType($input) {
  $output = Array();

  foreach($input as $value) {
    $output_element = & $output[$value['part'] . "_" . $value['type']];
    $output_element['part'] = $value['part'];
    $output_element['type'] = $value['type'];
    !isset($output_element['count']) && $output_element['count'] = 0;
    $output_element['count'] += $value['count'];
  }

  return array_values($output);
}</code>

By applying this function to the input array $arr1, we obtain the desired output $arr2. Each element in the output array represents a group of data points that share the same "part" and "type" values, along with the sum of the corresponding "count" values.

Alternative Approach:

If both database queries originate from the same database server, you could potentially use SQL's GROUP BY feature to perform the grouping and aggregation within the database itself, eliminating the need for post-processing in PHP.

The above is the detailed content of How do I Group Multidimensional Array Data by Multiple Columns and Aggregate Values in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn