Home >Backend Development >PHP Problem >How to group php arrays
PHP is one of the widely used languages, especially in web development, it is a highly flexible and easy-to-use language. Arrays in PHP are a very convenient and practical tool when we need to group data. In this article, we will take a closer look at how PHP arrays are grouped.
Step 1: Prepare data
Before performing grouping operations, we need to prepare data. This article uses student data in the student information management system. The specific data is as follows:
$students = [ [ "name" => "张三", "gender" => "男", "age" => 18, "score" => 85, "class" => "T1" ], [ "name" => "李四", "gender" => "女", "age" => 19, "score" => 80, "class" => "T2" ], [ "name" => "王五", "gender" => "男", "age" => 20, "score" => 95, "class" => "T1" ], [ "name" => "赵六", "gender" => "女", "age" => 18, "score" => 70, "class" => "T2" ], [ "name" => "钱七", "gender" => "男", "age" => 19, "score" => 90, "class" => "T1" ] ];
The above data includes the student's name, gender, age, grades and class information.
Step 2: Group according to class
Now, we will use the array_column function in PHP to group the above data according to class. The array_column function can get a subarray in a multidimensional array. For example, the following code will get a subarray named "class" from the $students array:
$class = array_column($students, 'class');
$class array will only contain all students' Class information. We can use array_unique function in PHP to get unique classes. This will provide the basis for us to group student information.
$unique_classes = array_unique($class);
Now that we have the array $unique_classes which contains a list of all unique classes, let’s use a foreach loop to iterate over the array and create a new multidimensional array $grouped_students and group the students in the same class The information is stored in a subarray.
$grouped_students = array(); foreach($unique_classes as $class_name){ $grouped_students[$class_name] = array(); foreach($students as $student){ if($student['class'] == $class_name){ $grouped_students[$class_name][] = $student; } } }
The above code will group all student information according to class and store it in a multi-dimensional array named $grouped_students. Now we can use var_dump or print_r to view the $grouped_students array and check if the grouping was successful. The following is the output:
Array ( [T1] => Array ( [0] => Array ( [name] => 张三 [gender] => 男 [age] => 18 [score] => 85 [class] => T1 ) [1] => Array ( [name] => 王五 [gender] => 男 [age] => 20 [score] => 95 [class] => T1 ) [2] => Array ( [name] => 钱七 [gender] => 男 [age] => 19 [score] => 90 [class] => T1 ) ) [T2] => Array ( [0] => Array ( [name] => 李四 [gender] => 女 [age] => 19 [score] => 80 [class] => T2 ) [1] => Array ( [name] => 赵六 [gender] => 女 [age] => 18 [score] => 70 [class] => T2 ) ) )
The above output shows that we have successfully grouped student information according to class.
Step 3: Group based on other fields
In addition to class, we can also group student information based on other fields. Here we will use age as the grouping criterion. The specific code is as follows:
$grouped_students_by_age = array(); foreach($students as $student){ $grouped_students_by_age[$student['age']][] = $student; }
The above code will group student information according to age and store it in a multi-dimensional array named $grouped_students_by_age. Now we can use var_dump or print_r to view the $grouped_students_by_age array and check if the grouping was successful. The following is the output:
Array ( [18] => Array ( [0] => Array ( [name] => 张三 [gender] => 男 [age] => 18 [score] => 85 [class] => T1 ) [1] => Array ( [name] => 赵六 [gender] => 女 [age] => 18 [score] => 70 [class] => T2 ) ) [19] => Array ( [0] => Array ( [name] => 李四 [gender] => 女 [age] => 19 [score] => 80 [class] => T2 ) [1] => Array ( [name] => 钱七 [gender] => 男 [age] => 19 [score] => 90 [class] => T1 ) ) [20] => Array ( [0] => Array ( [name] => 王五 [gender] => 男 [age] => 20 [score] => 95 [class] => T1 ) ) )
As you can see, we have successfully grouped student information according to age.
Summary
In this article, we introduced how to use PHP arrays to group data. We took student information as an example and grouped the data according to class and age. These operations can help us manage and process data more conveniently, and improve the efficiency and readability of the code. Finally, readers are encouraged to try other grouping methods and give full play to the advantages of PHP arrays in actual development.
The above is the detailed content of How to group php arrays. For more information, please follow other related articles on the PHP Chinese website!