Home >Backend Development >PHP Tutorial >How to sort a two-dimensional array using a certain key value in PHP
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 ), ];
Sort by 'age' key value:
array_multisort(array_column($arr,'age'),SORT_DESC,$arr); printr($arr);
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!