Detailed explanation of how to group two-dimensional arrays according to key in PHP

墨辰丷
Release: 2023-03-28 11:30:01
Original
2389 people have browsed it

This article mainly introduces the relevant information of PHP grouping two-dimensional arrays according to key. It is very good and has reference value. Friends who need it can refer to it

We often get a two-dimensional array. , you will find that the results are somewhat different from what you want. You may need to group the array according to a certain field in the two-dimensional array.

Let’s first look at the following array,


Array
(
[0] => Array
(
[id] => 1
[wo_id] => 2
[evaluate_id] => 1
[type] => 分组1
[ctime] => 2016-12-02 11:39:34
)
[1] => Array
(
[id] => 2
[wo_id] => 31
[evaluate_id] => 2
[type] => 分组1
[ctime] => 2016-12-12 11:39:50
)
[2] => Array
(
[id] => 3
[wo_id] => 31
[evaluate_id] => 4
[type] => 分组2
[ctime] => 2016-12-02 11:40:01
)
[3] => Array
(
[id] => 4
[wo_id] => 2
[evaluate_id] => 5
[type] => 分组2
[ctime] => 2016-12-07 10:21:40
)
[4] => Array
(
[id] => 5
[wo_id] => 2
[evaluate_id] => 3
[type] => 分组2
[ctime] => 2016-12-07 10:21:51
)
[5] => Array
(
[id] => 6
[wo_id] => 2
[evaluate_id] => 2
[type] => 分组3
[ctime] => 2016-12-07 10:21:56
)
[6] => Array
(
[id] => 7
[wo_id] => 4
[evaluate_id] => 2
[type] => 分组3
[ctime] => 2016-12-07 10:22:10
)
)
Copy after login


Here is a very typical example , if I want to process the data in the array separately according to type (grouping), if not grouped, the loop may be very redundant, so I have the following grouping function:


  public static function array_group_by($arr, $key)
{
$grouped = [];
foreach ($arr as $value) {
$grouped[$value[$key]][] = $value;
}
// Recursively build a nested grouping if more parameters are supplied
// Each grouped array value is grouped according to the next sequential key
if (func_num_args() > 2) {
$args = func_get_args();
foreach ($grouped as $key => $value) {
$parms = array_merge([$value], array_slice($args, 2, func_num_args()));
$grouped[$key] = call_user_func_array('array_group_by', $parms);
}
}
return $grouped;
}
Copy after login


$arr: two-dimensional array

$key: the key to be grouped

The result is:


Array
(
[分组2] => Array
(
[0] => Array
(
[id] => 1
[wo_id] => 2
[evaluate_id] => 1
[ctime] => 2016-12-02 11:39:34
[type] => 分组2
)
[1] => Array
(
[id] => 2
[wo_id] => 31
[evaluate_id] => 2
[ctime] => 2016-12-12 11:39:50
[type] => 分组2
)
[2] => Array
(
[id] => 3
[wo_id] => 31
[evaluate_id] => 4
[ctime] => 2016-12-02 11:40:01
[type] => 分组2
)
)
[分组1] => Array
(
[0] => Array
(
[id] => 4
[wo_id] => 2
[evaluate_id] => 5
[ctime] => 2016-12-07 10:21:40
[type] => 分组1
)
[1] => Array
(
[id] => 5
[wo_id] => 2
[evaluate_id] => 3
[ctime] => 2016-12-07 10:21:51
[type] => 分组1
)
)
)
Copy after login


The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP filter two-dimensional array and three-dimensional array, php two-dimensional array array

PHP filtering two-dimensional arrays and three-dimensional arrays, php two-dimensional array array_PHP tutorial

PHP filtering two-dimensional arrays and three-dimensional arrays, php two Dimension array array


The above is the detailed content of Detailed explanation of how to group two-dimensional arrays according to key in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!