PHP multi-dimensional array sorting maintains the index correspondence and uses uasort
Source: Internet
Author: User
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 a user-defined comparison function to sort the values in the array and maintain the index Association. multiple values can be sorted... "/> <scripttype =" text/javascript "src =" http://www.2 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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service