Home  >  Article  >  Backend Development  >  Application of PHP array grouping function in data aggregation

Application of PHP array grouping function in data aggregation

WBOY
WBOYOriginal
2024-05-01 11:00:021091browse

PHP array_group_by() function can group array elements according to the specified key, forming an array with the key as the index and the array as the value. For example, after grouping sales records according to the product field, the key in the grouped array is the product value, and the value is the array of sales records belonging to this product.

PHP 数组分组函数在数据聚合中的应用

Application of PHP array grouping function in data aggregation

Array grouping is a commonly used operation in data aggregation. The elements in an array can be grouped by a specified key to form a new array with the key as the index and the array as the value. PHP provides the array_group_by() function to implement array grouping.

Usage:

array_group_by(array $input, string $key)

Among them, $input is the array to be grouped, $key is the key to group by name.

Practical case:

We have an array containing the following sales records:

$sales = [
    ['product' => 'A', 'quantity' => 10, 'total' => 100],
    ['product' => 'B', 'quantity' => 20, 'total' => 200],
    ['product' => 'A', 'quantity' => 30, 'total' => 300],
    ['product' => 'C', 'quantity' => 40, 'total' => 400],
];

To compare sales based on the product field To group records, we can use the array_group_by() function:

$groupedSales = array_group_by($sales, 'product');

The result after grouping is an array, where the key is the product value, and the value belongs to this product Array of sales records:

print_r($groupedSales);

// 输出:
Array
(
    [A] => Array
        (
            [0] => Array
                (
                    [product] => A
                    [quantity] => 10
                    [total] => 100
                )
            [1] => Array
                (
                    [product] => A
                    [quantity] => 30
                    [total] => 300
                )
        )
    [B] => Array
        (
            [0] => Array
                (
                    [product] => B
                    [quantity] => 20
                    [total] => 200
                )
        )
    [C] => Array
        (
            [0] => Array
                (
                    [product] => C
                    [quantity] => 40
                    [total] => 400
                )
        )
)

By grouping, we can easily summarize the sales data of each product group or perform other data aggregation operations.

The above is the detailed content of Application of PHP array grouping function in data aggregation. 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