PHP Multidimensional Array Sort keeping index correspondence relation skillfully using Uasort

Source: Internet
Author: User
Tags array array sort arrays definition comparison copy string sort
In actual development, more or less will encounter array sorting problem, in addition to the conventional write simple sorting algorithm, PHP also provides a built-in array sorting function, this time to share the key: Uasort   Use user-defined comparison functions to sort values in an array and keep index associations, sortable multidimensional arrays , this article focuses on this function.             uasort function   parameter type: BOOL Uasort (array & $array, callable $cmp _function) & nbsp This function sorts the array and maintains an association between the index and the cell.   is primarily used to sort the associative arrays that are important to the order of the cells. The comparison function is user-defined. Returns TRUE when   succeeds, or returns FALSE on failure.       Array sorting instance (non Class):   Copy code  1/**  2  * custom sort function  3  * @param $param 1 &nbsp 4  * @param $param 2  5  * @return 0 (not moved) 1 (forward reversal sequence)-1 (reverse order)  6  */ 7   8 funct Ion My_sort ($param 1, $param 2) { 9     if ($param 1 = $param 2) return 0/    else return $param 1 > $param 2? 1:-1; 12  $arr = Array (            ' a ' => ',         & nbsp   ' B ' => ' 1 ',             ' C ' => ',           &NBSP ; ' d '=> ' 5 ',             ' e ' => ',             ' F ' => ' 4 ',             ' G ' => ' 3 ',     (   ); 22  uasort ($arr, ' my_sort '); 24  Var_dump ($arr); 26  27/* Output value (size=7)   ' B ' => string ' 1 ' (length=1)   ' G ' => string ' 3 ' (length=1) 31   ' 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):       Replication code/**  * Custom sort function  * @param $param 1  * @param $param 2  * @return 0 (No Move) 1 (forward switching 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 ' => '),     &NBS P       ' B ' =>array (' key ' => ' definition 2 ', ' Value ' => ' 1 '),           ' C ' =>a Rray (' key ' => ' definition 3 ', ' Value ' => '),             ' d ' =>array (' key ' => ' definition 4 ', ' Value ' => ' 5 '),             ' e ' =>array (' key ' => ' definition 5 ', ' Value ' => '),   & nbsp         ' 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 ' set Righteousness 2 ' (length=7)       ' value ' => string ' 1 ' (length=1)   ' G ' =>      Array (size=2 )     &NBsp ' 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)   &nbs P   ' key ' => string ' definition 3 ' (length=7)       ' value ' => string ' (length=2)   ' a ' =>&nbsp ;     Array (size=2)       ' key ' => string ' definition 1 ' (length=7)       ' value ' => s Tring ' (length=2)   ' e ' =>      Array (size=2)       ' key ' => string ' definition 5 ' (l ength=7)       ' value ' => string ' (length=2) * * * Copy code             class In order to facilitate a two-dimensional array ofExample:   Uasort ($arr 1, Array ($this, ' public_my_sort '));   Uasort ($arr 2, Array (' self ', ' self_my_sort '));   Copy code class myclasssort{        /**      * Sort Main method      * @pa Ram $arr 1  self static sort      * @param $arr 2  this sort      * @return sorted array   &NBSP ;  */    Public Function main ($arr 1 = Array (), $arr 2 = Array ()) {           &N Bsp Uasort ($arr 1, Array ($this, ' public_my_sort '));                  Uasort ($arr 2, Array (' self ', ' self_my_sort '));                  return Array (' arr1 ' => $arr 1, ' arr2 ' => $arr 2);         }         /**      * custom sort function     &NBS p;* @param $param 1      * @param $param 2      * @return 0 (no move) 1 (forward switching order)-1 (reverse order)   & nbsp */    private static function Self_my_sort ($param 1, $param 2) {        if ($param 1[' Val UE '] = = = $param 2[' value ']) return 0;         else return $param 1[' value '] > $param 2[' value ']? 1:-1;    }              //ditto     Public function Public_my_sort ($p ARAM1, $param 2) {        if ($param 1[' value ' = = $param 2[' value ') return 0;       &NBSP ; else return $param 1[' value '] > $param 2[' value ']? 1:-1;    }     }   $arr = Array (            ' a ' =>array (' key ' =& gt; ' Definition 1 ', ' value ' => '),             ' B ' =>array (' key ' => ' definition 2 ', ' Value ' => ' 1 '), & nbsp           ' C ' =>array (' key ' => ' definition 3 ', ' Value ' => '),           &N Bsp ' 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 '),        );          $myClassSort = new Myclasssort ();   Var_dump ($myClassSort->main ($arr, $arr));  /* Output with the above example/copy code         Similar function extension      array_multisort to sort multiple arrays or multidimensional arrays, but eventually fill in the used Or a specific one-dimensional array      arsort   to reverse sort and maintain index relationships of one-dimensional arrays, keeping index correspondence      asort   Forward ordering of one-dimensional arrays and keeping index relationships, keeping index correspondence      krsort   Logarithmic group in reverse order by key name, keeping index correspondence      ksort   Arrays are sorted alphabetically by key name, keeping index correspondence      natcasesort   Use the "natural sort" algorithm to sort the case-insensitive alphabetical array of one-dimensional arrays, which can be used to sort the alphanumeric mixture of the array contents, Keep index correspondence      natsort   Sorting a one-dimensional array using the "natural sort" algorithm, distinguishing between uppercase and lowercase letters, can be used to sort the case of alphanumeric mixing in the contents of an array, keeping index correspondence      rsort   Reverse order of one-dimensional array, do not keep index corresponding relationship &nbsp    sort   to one-dimensional array forward sorting, not keeping index correspondence      uasort   Use the user-defined comparison function to sort the values in the array and keep the index associated, and to 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   Use the user-defined comparison function to sort the values in the array, not keeping the index association    

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.