Quick Master PHP multi-dimensional array sorting method _php Tutorial

Source: Internet
Author: User
We are learning PHP, we may be a multidimensional array of PHP problems to be confused, the article here on the detailed introduction of PHP multi-dimensional array sorting principle. PHP also allows for more complex sorting on multidimensional arrays-for example, a single nested array is sorted by using a common keyword, and then sorted by another keyword. This is similar to sorting multiple fields with an order BY statement that uses SQL. To get a better idea of how it works, take a closer look at the examples:

 
 
  1. php $data = array(array ("id" => 1, "name" = > "Boney M", "rating" = > 3),
  2. Array ("id" => 2, "name" => "Take That", "rating" = > 1),
  3. Array ("id" => 3, "name" => "The Killers", "rating" => 4),
  4. Array ("id" => 4, "name" => "Lusain", "rating" => 3),
  5. ); foreach ($data as $key => $value) {
  6. $name [$key] = $value [' name '];
  7. $rating [$key] = $value [' rating '];
  8. }
  9. Array_multisort ($rating, $name, $data); Print_r ($data); ?>

The Array_multisort () function is one of the most useful functions in PHP, and it has a very wide range of applications. Also, as you can see in the example, it is possible to sort multiple unrelated arrays, use one of them as the basis for the next sort, and sort the database result set. These examples should give you a basic understanding of the use of the PHP multi-dimensional array sorting function, and show you some of the internal features hidden in the PHP array processing toolkit. Here, we simulate a row and column array in the $DATA array. I then use the Array_multisort () function to rearrange the data collection, starting with rating, and then sorting by name if the rating is equal. It outputs the following results:

 
 
  1. Array ([0] => array
  2. (
  3. [id] => 2
  4. [Name] = > Take That
  5. [Rating] = > 1
  6. ) [1] => Array
  7. (
  8. [id] => 1
  9. [Name] = > Boney M
  10. [Rating] = > 3
  11. )
  12. [2] => Array
  13. (
  14. [id] => 4
  15. [Name] = > Lusain
  16. [Rating] = > 3
  17. )
  18. [3] => Array
  19. (
  20. [id] => 3
  21. [Name] = > The killers
  22. [Rating] = > 4
  23. )
  24. )

http://www.bkjia.com/PHPjc/446444.html www.bkjia.com true http://www.bkjia.com/PHPjc/446444.html techarticle We are learning PHP, we may be a multidimensional array of PHP problems to be confused, the article here on the detailed introduction of PHP multi-dimensional array sorting principle. PHP is also allowed in ...

  • 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.