In PHP, many built-in functions are used to process arrays. in many cases, we can directly use their built-in functions to meet our needs and obtain the expected results. However, sometimes, we cannot implement our requirements by using built-in functions. Therefore, we need to write algorithms to implement our own ideas. The following describes how to sort two-dimensional arrays by keys. In PHP, many built-in functions are used to process arrays. in many cases, we can directly use their built-in functions to meet our needs and obtain the expected results. However, sometimes, we cannot implement our requirements by using built-in functions. Therefore, we need to write algorithms to implement our own ideas. The following describes how to sort two-dimensional arrays by keys.
Implementation method:
$ V) {$ key_value [$ k] = $ v [$ keys];} if ($ type = 'asc ') {asort ($ key_value );} else {arsort ($ key_value);} reset ($ key_value); foreach ($ key_value as $ k => $ v) {$ new_array [$ k] = $ arr [$ k];} return $ new_array;} $ arr = array ('name' => 'A ', 'Sex '=> 'm', 'sort' => 5), array ('name' => 'C', 'Sex '=>'m ', 'sort '=> 8), array ('name' => 'G', 'sex' => 'm', 'sort' => 3 ), array ('name' => 'e ', 'Sex '=> 'W', 'sort' => 6), array ('name' => 'B', 'Sex '=> 'W ', 'sort '=> 2),); // print the original array print_r ($ arr); // Print the sorted array print_r (array_sort ($ arr, 'name');?>
The output result shows that the array is sorted by "name.
// Original Array ([0] => Array ([name] => a [sex] => m [sort] => 5) [1] => Array ([name] => c [sex] => m [sort] => 8) [2] => Array ([name] => g [sex] => m [sort] => 3) [3] => Array ([name] => e [sex] => w [sort] => 6) [4] => Array ([name] => B [sex] => w [sort] => 2 )) // sorted Array ([2] => Array ([name] => g [sex] => m [sort] => 3) [3] => Array ([name] => e [sex] => w [sort] => 6) [1] => Array ([name] => c [sex] => m [sort] => 8) [4] => Array ([name] => B [sex] => w [sort] => 2) [0] => Array ([name] => a [sex] => m [sort] => 5 ))
The above is the PHP development technique (2)-details about how to sort two-dimensional arrays by key. For more information, see other related articles in the first PHP community!