PHP Array sorting +php two-dimensional array ordering method

Source: Internet
Author: User
Tags sorts

PHP-sorting functions for one-dimensional arrays

In this section, we will learn the following PHP array sorting functions:

    • Sort ()-sorts an array in ascending order
    • Rsort ()-sorts the array in descending order
    • Asort ()-sorts associative arrays in ascending order based on values
    • Ksort ()-Sorts the associative array in ascending order, based on the key
    • Arsort ()-sorts associative arrays in descending order by value
    • Krsort ()-Sorts the associative array in descending order, based on the key

One-dimensional array ordering can be Asort, Ksort, and other methods of process sequencing, relatively simple. How does the ordering of the two-dimensional arrays be implemented? Use Array_multisort and usort to achieve

For example like the following array: The code is as follows: $users = Array (' name ' = ' Tom ', ' age ' = +), array (' name ' = ' = ' Anny ', ' age ' = 1  8), Array (' name ' = ' Jack ', ' age ' = 22); I want to be able to sort by age from small to large. The author has collated two methods to come out, shares to everybody. 1, using Array_multisort Use this method, it will be more troublesome, to extract the age to store in a one-dimensional array, and then in ascending order of age. The code is as follows: $ages = Array (), foreach ($users as $user) {$ages [] = $user [' Age '];} array_multisort ($ages, SORT_ASC, $use   RS); After execution, $users is a sorted array and can be printed out to see. If you need to first sort by age, and then by name in ascending order, the same way, is to extract an array of names, the final sorting method called: The code is as follows: Array_multisort ($ages, SORT_ASC, $names, SORT_ASC, $ Users);2. Using Usort The biggest benefit of using this method is that you can customize some of the more complex sorting methods. For example, sort by name in descending order of length: The code is as follows:usort ($users, function ($a, $b) {$al = strlen ($a [' name ']);$BL = strlen ($b [' name ']);if ($al = = $BL)return 0;return ($al > $bl) -1:1;        });  anonymous functions are used here and can be extracted separately if necessary. A $ A, $b can be understood as an element under the $users array, you can directly index the name value, calculate the length, and then compare the length.  I prefer the second approach because there are fewer steps to extract the sorted content to a one-dimensional array, and the ordering method is more flexible.

PHP Array sorting +php two-dimensional array ordering method

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.