How to group php arrays according to the same value

PHPz
Release: 2023-04-14 19:01:14
Original
140 people have browsed it

PHP array is a very commonly used data type, which is often used in our development process. In actual development, we often encounter situations where we need to group based on the same values ​​in the array, so how to implement this?

In PHP, we can use array_column(), array_multisort() and other functions to sort and group arrays. Let's take a look at some specific implementation methods.

  1. Use the array_column() function to get the key name of the same value

First, we can use the array_column() function, which can take out a key from an array The corresponding value is also returned as an array. We can perform grouping operations based on the array returned by the array_column() function, which is a collection of the same values.

The specific implementation method is as follows:

<code class="php"><?php
// 定义一个数组
$arr = array(
    array('id'=>1, 'name'=>'张三', 'age'=>18),
    array('id'=>2, 'name'=>'李四', 'age'=>20),
    array('id'=>3, 'name'=>'张三', 'age'=>22),
    array('id'=>4, 'name'=>'王五', 'age'=>25),
    array('id'=>5, 'name'=>'赵六', 'age'=>20),
);

// 获取相同值的键名
$key_arr = array_column($arr, 'name');

// 分组操作
$res = array();
foreach($key_arr as $k=>$v) {
    $res[$v][] = $arr[$k];
}
print_r($res);
?></code>
Copy after login

In the above code, we define an array that contains multiple pieces of data. Then, we use the array_column() function to retrieve the values ​​corresponding to all the 'name' keys in the data, that is, ['Zhang San', 'Li Si', 'Zhang San', 'Wang Wu', 'Zhao Liu'].

Next, we use a foreach loop to traverse the above array, group data with the same value and store it in the $res array. Finally, the $res array is the grouping result we need.

  1. Use the array_multisort() function to sort multi-dimensional arrays

Another implementation method is to use the array_multisort() function to sort multi-dimensional arrays and sort data with the same value. together and then group operations.

The specific implementation method is as follows:

<code class="php"><?php
// 定义一个数组
$arr = array(
    array('id'=>1, 'name'=>'张三', 'age'=>18),
    array('id'=>2, 'name'=>'李四', 'age'=>20),
    array('id'=>3, 'name'=>'张三', 'age'=>22),
    array('id'=>4, 'name'=>'王五', 'age'=>25),
    array('id'=>5, 'name'=>'赵六', 'age'=>20),
);

// 对数组进行排序
$name_arr = array_column($arr, 'name');
$age_arr = array_column($arr, 'age');

array_multisort($name_arr, SORT_ASC, SORT_STRING, $age_arr, SORT_ASC, SORT_NUMERIC, $arr);

// 分组操作
$res = array();
foreach($arr as $k=>$v) {
    $res[$v['name']][] = $v;
}
print_r($res);
?></code>
Copy after login

In the above code, we use the array_column() function to obtain the values ​​corresponding to the 'name' and 'age' keys, and then use the array_multisort() function to sort the array Sort. The array_multisort() function can receive multiple array parameters, sort them in ascending or descending order according to the value of the first array, and then sort the other arrays accordingly according to the order of the first array. In this example, we set $name_arr to sort in ascending order and $age_arr to sort in ascending order.

Finally, we use a foreach loop to group data with the same value and store the results into the $res array. Finally, the $res array is the grouping result we need.

Summary

Both of the above two methods can realize the operation of grouping PHP arrays based on the same value. Which method to choose for specific implementation depends on your own needs and the structure of the data and other factors. No matter which method is used, we can achieve effective classification and statistics of arrays, thereby quickly improving the efficiency and reliability of data processing.

The above is the detailed content of How to group php arrays according to the same value. For more information, please follow other related articles on the PHP Chinese website!

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!