Home > Backend Development > PHP Problem > How to sum array groups in php

How to sum array groups in php

PHPz
Release: 2023-04-27 09:31:47
Original
607 people have browsed it

In the PHP programming language, we often need to operate on arrays. Among these operations, group summation of arrays is a very common requirement, especially in statistical analysis, data processing and financial application scenarios. This article will introduce how to use PHP to group and sum the following arrays.

First, we have an array $aSalesRecord containing product sales records. Each element is an array of sales records, including three fields: product name, selling price, and sales quantity. The array structure is as follows:

$aSalesRecord = array(
array('product_name' => 'A', 'price' => 10, 'quantity' => 2),
array('product_name' => 'B', 'price' => 5, 'quantity' => 3),
array('product_name' => 'A', 'price' = > 15, 'quantity' => 1),
array('product_name' => 'C', 'price' => 20, 'quantity' => 2),
array( 'product_name' => 'A', 'price' => 12, 'quantity' => 3),
array('product_name' => 'B', 'price' => 6, 'quantity' => 1),
array('product_name' => 'C', 'price' => 18, 'quantity' => 4),
array('product_name' = > 'D', 'price' => 25, 'quantity' => 2),
array('product_name' => 'E', 'price' => 30, 'quantity' = > 1),
);

Now, we need to group and sum this array, that is, group the sales records according to the product name, and calculate the sum of the sales amount in each group. This can be achieved in the following way:

$groupedSales = array();
foreach ($aSalesRecord as $record) {
$productName = $record['product_name'];
$price = $record['price'];
$quantity = $record['quantity'];
if (!isset($groupedSales[$productName])) {

$groupedSales[$productName] = array(
  'count' => 0,
  'total_price' => 0,
);
Copy after login

}
$groupedSales[$productName]['count'] = $quantity;
$groupedSales[$productName]['total_price'] = $price * $quantity;
}

Above In the code, we first define an empty array $groupedSales to save sales records grouped by product name. Then, we traverse the original sales record array $aSalesRecord. For each sales record, we take out the three fields of product name, selling price and sales quantity, and group them according to the product name. If the current product name does not exist in the $groupedSales array, create a new array element with the key as the product name, and initialize the total sales quantity and total sales amount to 0. Then, add the quantity of the current sales record to the total sales quantity, and add the sales amount of the current sales record to the total sales amount. In this way, after traversing all sales records, the $groupedSales array stores the sales records grouped by product name, and also contains the values ​​of the two fields of the total sales quantity and the total sales amount in each group.

In order to verify the correctness of the above code, we can use the var_dump() function to print the contents of the $groupedSales array, as shown below:

var_dump($groupedSales);

After running the above code, the following results will be output:

array(5) {
["A"]=>
array(2) {

["count"]=>
int(6)
["total_price"]=>
int(107)
Copy after login

}
["B"]=>
array(2) {

["count"]=>
int(4)
["total_price"]=>
int(24)
Copy after login

}
["C"]=>
array(2) {

["count"]=>
int(6)
["total_price"]=>
int(128)
Copy after login

}
["D"]=>
array(2) {

["count"]=>
int(2)
["total_price"]=>
int(50)
Copy after login

}
["E"]=>
array(2) {

["count"]=>
int(1)
["total_price"]=>
int(30)
Copy after login

}
}

As can be seen from the above output, the $groupedSales array contains sales records grouped by product name, and the sales within each group are also calculated. The sum of the amount and the sum of the sales quantity.

In general, grouping and summing arrays in PHP is a very simple matter. You only need to traverse the original array, and then process each element as needed and assign it to the corresponding In the group, finally count the sum of the elements in each group. For some more complex grouped sum operations, you can also use other advanced data types of PHP (such as hash tables, trees, etc.) to implement. Either way, it's important to take full advantage of PHP's powerful features to improve programming efficiency and code readability.

The above is the detailed content of How to sum array groups in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template