Old bird talk about PHP array sorting

Source: Internet
Author: User

PHP is still quite common, So I studied the PHP array sorting, and I will share it with you here, hoping it will be useful to you. After learning about usort custom sorting, let's take a look at sort (). This function is the originator of the sorting function in the array, you will surely find that all PHP array sorting functions are suffixed With sort. Function prototype: bool sort (array & array [, int sort_flags]) Description: basically, each function has an optional parameter, and sort is no exception. This optional parameter specifies a habit.

The following types are available:
◆ SORT_REGULAR-normal comparison Unit (without changing the type) // sort by ASCII value (B is greater than)
◆ SORT_NUMERIC-the unit is used as a number to compare // this parameter is commonly used for integers and floating-point numbers.
◆ SORT_STRING-the Unit is compared as a string
◆ SORT_LOCALE_STRING-compare the unit as a string based on the current region (locale) settings

Let's take a look at the example from the help manual:

 
 
  1. php 
  2. $fruits = array("lemon", "orange", "banana", "apple");  
  3. sort($fruits);  
  4. foreach ($fruits as $key => $val) {  
  5. echo "fruits[".$key."] = " . $val . "n";  
  6. }  
  7. ?> 

The result of sorting the PHP array is as follows:

 
 
  1. fruits[0] = apple  
  2. fruits[1] = banana  
  3. fruits[2] = lemon  
  4. fruits[3] = orange 

You can find that the lemon with an index of 0 is then sorted and then changed to apple. To keep the original key/value unchanged, you can replace sort () with asort. These two functions sort keys in ascending order (a is prior to B ). To make it in descending order, you only need to use rsort () instead. The corresponding arsort () maintains the relevance of the original key/value.

The following describes the ksort () function.

This function is mainly used to exist associated arrays. Its "sister" function uksort () is compared using a custom function. In a word, ksort () sorts keys for the correlation level and retains the association from key name to Data. The natural sorting of arrays. The sorting functions we know so far are either sorted by default or by custom means. Natsort () uses a user-friendly sorting method to sort arrays. If you have no PHP help manual on hand, you can view the online help manual on this site.

 
 
  1. Shuffle () // function usage
  2. Shuffle () // is used to sort an array randomly. This is a bit like drawing lots, so we will not talk about anything else. Let's take a look.
  3. Array_rand () // function usage
  4. The array_rand () // function is similar to the shuffle () function. It is also an element in the random return array. The following call is performed:
  5. Array_rand (array [, int num_req]) // optional parameter indicates the number of returned results.

At this point, I think we have some knowledge about sorting arrays. If there is anything else, let's make up for it later.


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.