Quick grasp of PHP multi-dimensional array sorting methods

Source: Internet
Author: User

When we are learning PHP, we may be confused about the multi-dimensional array sorting of PHP. The article will introduce the principle of multi-dimensional array sorting of PHP in detail here. PHP also allows you to perform complex sorting on multi-dimensional arrays. For example, you can first sort a nested array by a common keyword and then sort it by another keyword. This is very similar to sorting multiple fields using the SQL ORDER BY statement. To better understand how it works, take a closer look at the example:

 
 
  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.  
  10. array_multisort($rating, $name, $data); print_r($data);?> 

Array_multisort () is one of the most useful functions in PHP and has a wide range of applications. In addition, as you can see in the example, it can sort multiple irrelevant arrays, or use one of the elements as the basis for next sorting, you can also sort database result sets. These examples should give you a preliminary understanding of the use of the PHP multi-dimensional array sorting function, and show you some internal functions hidden in the PHP Array Processing toolkit. Here, we simulate an array of rows and columns in the $ data array. Then, I use the array_multisort () function to rearrange the data set. First, sort the data set by rating, and then sort the data by name if rating is equal. The output result is as follows:

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

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.