How to sort a two-dimensional array by a certain key value in PHP

*文
Release: 2023-03-18 09:12:01
Original
7750 people have browsed it

In practical applications, sometimes we encounter the need for specific sorting of two-dimensional arrays. How should we deal with this? Let’s take a look at the article’s examples!

$arr=[ array( 'name'=>'小坏龙', 'age'=>28 ), array( 'name'=>'小坏龙2', 'age'=>14 ), array( 'name'=>'小坏龙3', 'age'=>59 ), array( 'name'=>'小坏龙4', 'age'=>23 ), array( 'name'=>'小坏龙5', 'age'=>23 ), array( 'name'=>'小坏龙6', 'age'=>21 ), ]; array_multisort(array_column($arr,'age'),SORT_DESC,$arr); print_r($arr);
Copy after login

where array_column (array, a key value in the array) takes out a column of a key value from a multi-dimensional array and returns a one-dimensional array;

array_multisort (Array (one-dimensional array), sorting method (SOTR_ASC, SOTR_DESC), other arrays (can be two-dimensional))


The above is the detailed content of How to sort a two-dimensional array by a certain key value in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
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!