Home  >  Article  >  Backend Development  >  How to sort a two-dimensional array by a certain key value in PHP

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

*文
*文Original
2017-12-22 15:17:137835browse

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);

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!

Statement:
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