Two-dimensional PHP array sorting method

Source: Internet
Author: User
Tags sort sorted by name strlen

  one-dimensional array sorting can be sorted by some method processes, such as Asort, Ksort, and relatively simple. How does the order of two-dimensional arrays come true? Use Array_multisort and Usort to implement

For example, like the following array:     code as follows: $users = Array (    Array (' name ' => ' Tom ', ' age ' =>)    , ARRA Y (' name ' => ' Anny ', ' age ' =>]    , array (' name ' => ' Jack ', ' age ' => 22));     Want to sort by age from small to large. The author has collated two methods to come out and share to everybody.   1, using Array_multisort   Use this method, will be more trouble, to extract the age to store in a one-dimensional array, and then in ascending order of the year. The specific code is as follows:   code as follows: $ages = Array (); foreach ($users as $user) {    $ages [] = $user [' Age '];}   Array_multisort ($ages, SORT_ASC, $users);       Execution, $users is a sorted array, can be printed out to see. If you need to first in ascending order by age, and then sorted by name in ascending order, the method above, is to extract an array of names, the final sort method to call:   code as follows: Array_multisort ($ages, SORT_ASC, $names, Sort_ ASC, $users);     2, using Usort   The biggest advantage of using this method is that you can customize some of the more complex sorting methods. For example, by the length of the name descending order: The   code is as follows: Usort ($users, function ($a, $b) {            $al = strlen ($a [' NA Me ']);             $BL = strlen ($b [' name ']);             if ($al = = $BL)                return 0;             return ($al > $bl) -1:1;        });     The anonymous function is used here and can be extracted separately if necessary. Where $a, $b can be understood as the elements under the $users array, you can directly index the name value, and calculate the length, and then compare the length can be.   I prefer the second approach because less steps are taken to extract the sorted content into a one-dimensional array, and the sorting method is more flexible.

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.