The previous description of the PHP multidimensional array sort of a function array_multisort (), want to know people can click
Two-dimensional array sorting array_multisort
The following describes the sorting of multidimensional arrays that do not apply to Array_multisort ().
Here are the next 2 php sorting functions, one is asort, and the other is arsort.
The Asort (array,sorttype) function sorts the array and maintains the index relationship. It is primarily used to sort the associative arrays that are important for the cell order.
The Second optional parameter contains an additional sort ID.
- Sort_regular-Default. Processed in their original type (without changing the type).
- Sort_numeric-Handle the value as a number
- Sort_string-handles the value as a string
- Sort_locale_string-handles the value as a string, based on local settings.
Returns TRUE if successful, otherwise FALSE.
The Arsort (array,sorttype) function reverses the array and maintains the index relationship. It is primarily used to sort the associative arrays that are important for the cell order. This function is used in the same way as Asort, except that this is an inverse sort of the index of the array.
The sorting algorithm for two-dimensional arrays is given below:
<?php function Array_sort ($arr, $key, $type = ' asc ') {$keyvalues = $new _array = Array (); foreach ($arr as $k + = $v) { $keyvalues [$k] = $v [$key];} if ($type = = ' asc ') {asort ($keyvalues);} Else{arsort ($keyvalues);} foreach ($keyvalues as $k = + $v) {$temparray [$k] = $arr [$k];} return $temparray; } $student = Array (' name ' = ' Zhang San ', ' age ' = ', ' weight ' =>100, ' height ' =>180), Array (' name ' = = ' John Doe ', ' Age ' = ' + ', ' weight ' =>200, ' height ' =>150), Array (' name ' = ' Harry ', ' age ' = '-', ' weight ' =>150, ' Height ' =>165 ', Array (' name ' = ' = ' Zhao Liu ', ' age ' = ' + ', ' weight ' =>90, ' height ' =>173), Array (' name ' = ' Sun seven ', ' Age ' = ' + ', ' weight ' =>160, ' height ' =>170); $studentlist = Array_sort ($student, ' weight '); Var_dump ($ Studentlist);
by Asort This function, the index ordering of the two-dimensional array is preserved, and then the index corresponding to the sorted two-dimensional array is obtained.