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

不言
Release: 2023-04-03 09:20:02
Original
4065 people have browsed it

The sorting of a two-dimensional array in php can be implemented by multiple methods. For example, the following two-dimensional array is implemented by sorting the 'age' key value.

Two-dimensional array:

$arr=[    
array(        
        'name'=>'A',        
        'age'=>28
    ),    
array(        
    'name'=>'B',        
    'age'=>14
    ),
array(        
    'name'=>'C',        
    'age'=>59
    ),    
array(        
    'name'=>'D',        
    'age'=>23
    ),    
array(        
    'name'=>'E',        
    'age'=>23
    ),    
array(        
    'name'=>'F',        
    'age'=>21
    ),
];
Copy after login

Sort by 'age' key value:

array_multisort(array_column($arr,'age'),SORT_DESC,$arr);
printr($arr);
Copy after login

Among them array_column (array, A key value in the array) is a column that represents a key value taken 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 (which can be two-dimensional) can be used to sort multiple arrays at once, or to sort multi-dimensional arrays according to one or more dimensions.

Related recommendations:

Simple implementation method of PHP two-dimensional array sorting, PHP two-dimensional array sorting

PHP two-dimensional array sorting

The above is the detailed content of How to sort a two-dimensional array using 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template