Home  >  Article  >  Backend Development  >  How to group array elements according to specified rules in PHP

How to group array elements according to specified rules in PHP

WBOY
WBOYOriginal
2023-07-12 10:11:022317browse

How to group array elements according to specified rules in PHP

In PHP, we often need to group the elements in the array according to certain rules to make it easier to process and manage data. In this article, we'll cover a few common ways to accomplish this.

Method 1: Use foreach loop and new array

This method is more intuitive. By traversing the original array, the elements are placed into the new grouped array according to the specified rules.

//原始数组
$students = array(
    array('name' => '张三', 'score' => 80),
    array('name' => '李四', 'score' => 90),
    array('name' => '王五', 'score' => 70),
    array('name' => '赵六', 'score' => 85),
    array('name' => '孙七', 'score' => 95),
);

//新的分组数组
$groupedStudents = array();

foreach ($students as $student) {
    $score = $student['score'];
    if (!isset($groupedStudents[$score])) {
        $groupedStudents[$score] = array();
    }
    $groupedStudents[$score][] = $student;
}

//打印结果
print_r($groupedStudents);

The above code will group students according to their grades and print out the results. The output result is as follows:

Array(
    [80] => Array(
        [0] => Array(
            [name] => 张三
            [score] => 80
        )
    )
    [90] => Array(
        [0] => Array(
            [name] => 李四
            [score] => 90
        )
    )
    [70] => Array(
        [0] => Array(
            [name] => 王五
            [score] => 70
        )
    )
    [85] => Array(
        [0] => Array(
            [name] => 赵六
            [score] => 85
        )
    )
    [95] => Array(
        [0] => Array(
            [name] => 孙七
            [score] => 95
        )
    )
)

Method 2: Use the array_reduce function

The array_reduce function can accumulate the elements in the array according to the specified rules. We can use it to implement array grouping.

$groupedStudents = array_reduce($students, function ($result, $student) {
    $score = $student['score'];
    if (!isset($result[$score])) {
        $result[$score] = array();
    }
    $result[$score][] = $student;
    return $result;
}, array());

//打印结果
print_r($groupedStudents);

Method 3: Use the array_chunk function

The array_chunk function can split the array into multiple small arrays according to the specified size. We can use it to implement array grouping.

$groupedStudents = array_chunk($students, 1, true);
//打印结果
print_r($groupedStudents);

In the above code, we divide the original array into small arrays with each sub-array containing only one element, thereby achieving the effect of grouping according to the specified rules.

Through the above method, we can easily group array elements according to specified rules. For different application scenarios, we can choose a method that suits us to implement array grouping and improve the readability and maintainability of the code. I wish you can flexibly use array grouping techniques in PHP development and successfully realize your needs.

The above is the detailed content of How to group array elements according to specified rules 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