In actual development, you may encounter array sorting problems more or less. In addition to writing simple sorting algorithms, PHP also provides built-in array sorting functions. This article will focus on the following: uasort uses the User-Defined comparison function to sort the values in the array and maintain the index association. This function can be used to sort multi-dimensional arrays. This article focuses on this function. Uasort function parameter type: bool uasort (array & $ array, callable $ cmp_function) This function sorts arrays and maintains the association between indexes and units. It is mainly used to sort the arrays that are very important to the unit order. Comparison functions are user-defined. Returns TRUE if the call succeeds, or FALSE if the call fails. Array sorting instance (not in class ): copy code 1/** 2 * Custom sorting function 3 * @ param $ param1 4 * @ param $ param2 5 * @ return 0 (do not move) 1 (Forward order) -1 (Reverse Order) 6 */7 8 function my_sort ($ param1, $ param2) {9 if ($ param1 = $ param2) return 0; 10 else return $ param1> $ param2? 1:-1; 11} 12 13 $ arr = array (14 'A' => '20', 15 'B' => '1 ', 16 'c' => '10', 17 'D' => '5', 18 'E' => '21', 19 'F' => '4 ', 20 'G' => '3', 21); 22 23 uasort ($ arr, 'My _ sort '); 24 25 var_dump ($ arr ); 26 27/* output value 28 array (size = 7) 29 'B' => string '1' (length = 1) 30 'G' => string '3' (length = 1) 31 'F' => string '4' (length = 1) 32 'D' => string '5' (length = 1) 33 'c' => string '10' (length = 2) 34 'A' => string '20' (length = 2) 35 'E' => s Tring '21' (length = 2) 36 */copy the code multi-dimensional array sorting instance (not in class ): copy the code/*** custom sorting function * @ param $ param1 * @ param $ param2 * @ return 0 (do not move) 1 (Forward order switching) -1 (reverse switching order) */function my_sort ($ param1, $ param2) {if ($ param1 ['value'] = $ param2 ['value']) return 0; else return $ param1 ['value']> $ param2 ['value']? 1:-1 ;}$ arr = array ('A' => array ('key' => 'define 1', 'value' => '20 '), 'B' => array ('key' => 'define 2', 'value' => '1 '), 'c' => array ('key' => 'define 3', 'value' => '10 '), 'D' => array ('key' => 'define 4', 'value' => '5 '), 'E' => array ('key' => 'define 5', 'value' => '21 '), 'F' => array ('key' => 'define 6', 'value' => '4 '), 'G' => array ('key' => 'define 7', 'value' => '3'),); uasort ($ arr, 'My _ sort '); var_dump ($ arr);/* output value array (size = 7)' B '=> array (size = 2) 'key' => String 'defines 2' (length = 7) 'value' => string '1' (length = 1) 'G' => array (size = 2) 'key' => string' defines 7' (length = 7) 'value' => string '3' (length = 1) 'F' => array (size = 2) 'key' => string' defines 6' (length = 7) 'value' => string '4' (length = 1) 'D' => array (size = 2) 'key' => string' defines 4' (length = 7) 'value' => string '5' (length = 1) 'c' => array (size = 2) 'key' => string' defines 3' (length = 7) 'value' => string '10' (Length = 2) 'A' => array (size = 2) 'key' => string 'defines 1' (length = 7) 'value' => string '20' (length = 2) 'E' => array (size = 2) 'key' => string 'defines 5' (length = 7) 'value' => string '21' (length = 2) */copy the sorting in the Code class. For convenience, take a two-dimensional array as an example: uasort ($ arr1, array ($ this, 'Public _ my_sort '); uasort ($ arr2, array ('self', 'Self _ my_sort ')); copy the code class myClassSort {/*** main sorting method * @ param $ arr1 self static sorting * @ param $ arr2 this sorting * @ return Sorted array */public function main ($ arr1 = array (), $ arr2 = array () {uasort ($ arr1, array ($ this, 'Public _ my_sort '); uasort ($ arr2, array ('self', 'Self _ my_sort '); return array ('arr1' => $ arr1, 'arr2 '=> $ arr2);}/*** custom sorting function * @ param $ param1 * @ param $ param2 * @ return 0 (do not move) 1 (Forward switching order)-1 (reverse switching order) */private static function self_my_sort ($ param1, $ param2) {if ($ param1 ['value'] = $ param2 ['value']) return 0; Else return $ param1 ['value']> $ param2 ['value']? 1:-1;} // same as public function public_my_sort ($ param1, $ param2) {if ($ param1 ['value'] = $ param2 ['value']) return 0; else return $ param1 ['value']> $ param2 ['value']? 1:-1 ;}$ arr = array ('A' => array ('key' => 'define 1', 'value' => '20 '), 'B' => array ('key' => 'define 2', 'value' => '1 '), 'c' => array ('key' => 'define 3', 'value' => '10 '), 'D' => array ('key' => 'define 4', 'value' => '5 '), 'E' => array ('key' => 'define 5', 'value' => '21 '), 'F' => array ('key' => 'define 6', 'value' => '4 '), 'G' => array ('key' => 'define 7', 'value' => '3'),); $ myClassSort = new myClassSort (); var_dump ($ myClassSort-> main ($ arr, $ arr )); /* the output result is the same as the above instance */The copy code is similar to the Function Extension array_multisort to sort multiple arrays or multi-dimensional arrays, however, in the end, the specific one-dimensional array arsort is used to sort the one-dimensional array in reverse order and maintain the index relationship. asort is used to sort the one-dimensional array in forward order and maintain the index relationship, keep the index ing relationship krsort sorts the array in reverse order by key name, and keep the index ing relationship ksort sorts the array in forward order by key name, natcasesort uses the "natural sorting" algorithm to sort one-dimensional arrays by case-insensitive letters. It can be used to sort the combination of letters and numbers in the array content, natsort uses the "natural sorting" algorithm to sort one-dimensional arrays, which are case sensitive and can be used to sort the combination of letters and numbers in the array content, maintains the index correspondence. rsort sorts the one-dimensional array in reverse order, but does not maintain the index correspondence. sort sorts the one-dimensional array in forward order, does not maintain the index ing relationship uasort uses the User-Defined comparison function to sort the values in the array and maintain the index Association. Multi-dimensional arrays can be sorted, this article focuses on the usage of uksort to sort the key names in the array using the User-Defined comparison function. usort uses the User-Defined comparison function to sort the values in the array without keeping the Index associated.