Home  >  Article  >  Backend Development  >  Detailed explanation of how to group PHP arrays by multiple fields

Detailed explanation of how to group PHP arrays by multiple fields

WBOY
WBOYOriginal
2024-05-04 15:36:011172browse

How to group PHP array by multiple fields? array_column(): Group by the specified field and extract the value of the field as the key. array_multisort(): Sort an array by multiple fields, grouping adjacent duplicate rows. Custom function: Define a custom function and use a loop to group arrays by specified fields.

PHP 数组按多个字段分组的方法详解

#Detailed explanation of the method of grouping PHP arrays by multiple fields

Grouping arrays is a common operation for processing complex data sets. Often, you need to group array elements based on one or more fields to facilitate data processing or aggregation. PHP provides several ways to achieve this.

array_column()

array_column() Function is used to extract the value of a specific column from an array. It can be used to group arrays by extracting the field values ​​used for grouping and using them as keys.

$data = [
    ['name' => 'John', 'city' => 'New York'],
    ['name' => 'Jane', 'city' => 'London'],
    ['name' => 'Bob', 'city' => 'New York'],
];

// 按城市分组
$grouped = array_column($data, 'city');

// 输出结果
print_r($grouped);

Output:

[
    'New York' => [
        ['name' => 'John', 'city' => 'New York'],
        ['name' => 'Bob', 'city' => 'New York'],
    ],
    'London' => [
        ['name' => 'Jane', 'city' => 'London'],
    ],
]

array_multisort()

array_multisort() The function is used to sort arrays by multiple fields. It can also be used to group arrays by sorting the array by multiple fields and then grouping adjacent duplicate rows.

$data = [
    ['name' => 'John', 'city' => 'New York', 'age' => 25],
    ['name' => 'Jane', 'city' => 'London', 'age' => 30],
    ['name' => 'Bob', 'city' => 'New York', 'age' => 28],
];

// 按城市和年龄分组
array_multisort(array_column($data, 'city'), SORT_ASC, array_column($data, 'age'), SORT_ASC, $data);

// 输出结果
print_r($data);

Output:

[
    ['name' => 'John', 'city' => 'New York', 'age' => 25],
    ['name' => 'Bob', 'city' => 'New York', 'age' => 28],
    ['name' => 'Jane', 'city' => 'London', 'age' => 30],
]

Custom function

In some cases, you can define your own custom function to group arrays. Here is an example of a custom function that groups an array by multiple fields:

function group_by_fields(array $data, array $fields)
{
    $grouped = [];
    foreach ($data as $row) {
        $key = '';
        foreach ($fields as $field) {
            $key .= $row[$field] . ',';
        }
        $key = trim($key, ',');
        $grouped[$key][] = $row;
    }
    return $grouped;
}

$data = [
    ['name' => 'John', 'city' => 'New York', 'age' => 25],
    ['name' => 'Jane', 'city' => 'London', 'age' => 30],
    ['name' => 'Bob', 'city' => 'New York', 'age' => 28],
];

// 按城市和年龄分组
$grouped = group_by_fields($data, ['city', 'age']);

// 输出结果
print_r($grouped);

Output:

[
    'New York,25' => [['name' => 'John', 'city' => 'New York', 'age' => 25]],
    'New York,28' => [['name' => 'Bob', 'city' => 'New York', 'age' => 28]],
    'London,30' => [['name' => 'Jane', 'city' => 'London', 'age' => 30]],
]

The above is the detailed content of Detailed explanation of how to group PHP arrays by multiple fields. 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