Actual development, more or less will encounter array sorting problem, in addition to the regular write simple sorting algorithm, PHP also provides a built-in array sorting function, this focus on sharing: Uasort use a user-defined comparison function to sort the values in the array and keep the index association, you can sort the multidimensional array, This article focuses on this function. Uasort function parameter type: BOOL Uasort (array & $array, callable $cmp _function) This function sorts the array and maintains the association between the index and the cell. It is primarily used to sort the associative arrays that are important for the cell order. The comparison function is user-defined. Returns TRUE on success, or FALSE on failure. Array sort instance (non-Class): Copy code 1/** 2 * Custom sort function 3 * @param $param 1 4 * @param $param 2 5 * @return 0 (not moving) 1 (positive swap order) 1 (reverse order) 6 */ 7 8 function My_sort ($param 1, $param 2) {9 if ($param 1 = = $param 2) return 0;10 else return $param 1 > $param 2? 1: -1;11 }12 $arr = Array (+ ' a ' = ' + ', the ' B ' + ' + ' 1 ', "C ' + ' + '), and ' d ' and ' 5 ', ' e ' = ' + ', ' f ' = ' 4 ', ' G ' =& gt; ' 3 ', +); Uasort ($arr, ' my_sort '); Var_dump ($arr); 26 27 */Output value (size=7) ' B ' = ' 1 ' (length=1) 3 0 ' g ' = + string ' 3 ' (length=1) to ' f ' + = String ' 4 ' (length=1) + ' d ' + string ' 5 ' (length=1) ' c ' = = String ' Length=2 ' a ' + string ' (length=2) ' e ' = ' + String ' (length=2) 36 * * Copy code Multidimensional array sort instance (non-CLASS): Copy Code/** * Custom Sort function * @param $param 1 * @param $param 2 * @return 0 (not moving) 1 (positive swap Order)-1 (reverse order) */function My_sort ($param 1, $param 2) {if ($param 1[' value ') = = $param 2[' value ']) return 0; else return $param 1[' value ' > $param 2[' value ']? 1:- 1;} $arr = Array (' A ' =>array (' key ' = = ' definition 1 ', ' value ' = ') ', ' B ' =>array (' key ' = ' = ' definition 2 ', ' value ' = ' 1 '), ' c ' = >array (' key ' = ' = ' definition 3 ', ' value ' = ' + '), ' d ' =>array (' key ' = = ' definition 4 ', ' value ' = ' 5 '), ' E ' =>array (' key ') = = ' definition 5 ', ' value ' = ' + '), ' F ' =>array (' key ' = ' = ' = ' definition 6 ', ' value ' = ' 4 '), ' G ' =>array (' key ' = = ' definition 7 ', ' Value ' = ' 3 '),); Uasort ($arr, ' my_sort '); Var_dump ($arr); /* Output value Array (size=7) ' b ' = = Array (size=2) ' key ' = = String ' definition 2 ' (length=7) ' value ' = = String ' 1 ' (length=1) ' g ' = > Array (size=2) ' key ' = + string ' definition 7 ' (length=7) ' value ' = = String ' 3 ' (length=1) ' f ' = = Array (size=2) ' key ' = = String ' definition 6 ' (length=7) ' value ' = = String ' 4 ' (length=1) ' d ' = = Array (size=2) ' key ' =String ' definition 4 ' (length=7) ' value ' = = String ' 5 ' (length=1) ' c ' = = = Array (size=2) ' key ' = = String ' definition 3 ' (length=7) ' Value ' = ' + String ' (length=2) ' a ' = = Array (size=2) ' key ' = = String ' definition 1 ' (length=7) ' value ' = = String ' 20 ' (length=2) ' E ' = = Array (size=2) ' key ' = = String ' definition 5 ' (length=7) ' value ' = = String ' (length=2) */Copy code class in order to facilitate a two-dimensional array For example: Uasort ($arr 1, Array ($this, ' public_my_sort ')); Uasort ($arr 2, Array (' self ', ' self_my_sort ')); Copy code class myclasssort{/** * Sort Main Method * @param $arr 1 self static sort * @param $arr 2 This sort * @return sorted array * * Public Function Main ( $arr 1 = Array (), $arr 2 = Array ()) {Uasort ($arr 1, Array ($this, ' Public_my_sort ')), Uasort ($arr 2, Array (' self ', ' self_my_so RT ')); Return Array (' arr1 ' = $arr 1, ' arr2 ' = 2 $arr); }/** * Custom sort function * @param $param 1 * @param $param 2 * @return 0 (not moving) 1 (positive swap Order)-1 (reverse order) */private static function Self_my _sort ($param 1, $param 2) {if ($param 1[' value '] = = $param 2[' value ')) return 0; else return $param 1[' value '] > $param 2[' value '? 1:-1; }//Ibid. public Function Public_my_sort ($param 1, $param 2) {if ($param 1[' value '] = = $param 2[' value ')) return 0; else return $p aram1[' value '] > $param 2[' value ')? 1:-1; }} $arr = Array (' A ' =>array (' key ' = = ' definition 1 ', ' value ' = ') ', ' B ' =>array (' key ' = ' = ' definition 2 ', ' value ' = ' 1 '), ' C ' =>array (' key ' = ' = ' definition 3 ', ' value ' = ' + '), ' d ' =>array (' key ' = ' = ' definition 4 ', ' value ' = ' 5 '), ' E ' =>array (' Key ' = = ' defines 5 ', ' value ' = ' + '), ' F ' =>array (' key ' = ' = ' definition 6 ', ' value ' = ' 4 '), ' G ' =>array (' key ' = = ' definition 7 '), ' Value ' = ' 3 '),); $myClassSort = new Myclasssort (); Var_dump ($myClassSort->main ($arr, $arr)); /* Output same as above instance */Copy code like function extension array_multisort multiple arrays or multidimensional arrays, but eventually fill in the use of a specific one-dimensional array arsort The one-dimensional array to reverse the order and maintain the index relationship, keep the index correspondence asort To sort a one-dimensional array and keep the index relationship, keep the index correspondence krsort the array is reversed according to the key name, keep the index correspondence ksort the array is sorted by key name, keep the index correspondence natcasesort with "natural sort" The algorithm makes a case-insensitive sort of one-dimensional array, which can be used to sort the mixed alphanumeric cases of the array contents, and to keep the index correspondence natsort the one-dimensional array is sorted by the "natural sort" algorithm, and the uppercase and lowercase letters can be used to sort the mixed letters and numbers in the contents of the array. Keep index correspondence Rsort reverse-sort one-dimensional array, without retaining the cableReference relations sort to a one-dimensional array forward ordering, do not maintain index correspondence uasort use user-defined comparison function to sort the values in the array and keep the index association, can sort the multidimensional array, this article focuses on this function uksort using the user-defined comparison function to sort the key names in the array Usort uses a user-defined comparison function to sort the values in the array without keeping the index association
http://www.bkjia.com/PHPjc/767108.html www.bkjia.com true http://www.bkjia.com/PHPjc/767108.html techarticle in actual development, more or less will encounter array sorting problem, in addition to the regular write simple sorting algorithm, PHP also provides a built-in array sorting function, this focus on sharing: UAS ...