Veteran birds talk about PHP array sorting _php tutorial

Source: Internet
Author: User
Tags array sort locale setting shuffle
PHP is still more commonly used, so I studied the PHP array sorting, here to take out and share with you, hope to be useful to everyone. After learning about the Usort custom sort, let's look at sort (), which is the originator of the sort function in the array, and you'll definitely find that all the PHP array sort functions have a sort suffix. Function prototypes: bool Sort (array &array [, int sort_flags]) Description: Basically each function will have an optional parameter, sort is no exception. This optional parameter indicates a habit.

There are several types of options available:
sort_regular– Normal comparison unit (does not change type)//Sort by ASCII value (b is greater than a)
The sort_numeric– unit is used as a number to compare//for integers and floating-point numbers.
The sort_string– unit is compared as a string.
sort_locale_string– the cell as a string compared to the current locale setting

Or look at an 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 this sort of PHP array is as follows:

 
  
  
  1. Fruits[0] = Apple
  2. FRUITS[1] = Banana
  3. FRUITS[2] = Lemon

As you can see, the lemon, which was indexed at 0, became Apple after sorting. To keep the original key/value constant, you can change the sort () to Asort (). In addition, the two functions are arranged in ascending order of the keys (a in front of B). If you want to make it descending, as long as you use Rsort () instead, the corresponding arsort () is to keep the original key/value associated.

Below is a brief introduction to the following Ksort () function

This function is primarily used for associative arrays, and its "sister" function Uksort () is compared using a custom function. Bottom line: Ksort () for association progression, sorts the keys and preserves the key name-to-data association. The natural ordering of arrays, so far as we know the sort function is either in the default way, or in a custom way to sort. Then Natsort () uses a humane sort method to sort the array. In this case, you can view the manual, if you do not have the PHP help manual, you can browse the site of the online Help manual to view

 
  
  
  1. Shuffle ()//use of functions
  2. Shuffle ()//used to randomly sort an array, this is a bit like a lottery, and the rest is not much to say. Look at it when it's time to use it.
  3. Array_rand ()//use of functions
  4. Array_rand ()//function is similar to shuffle () and is also a random return element in an array, such as a downgrade:

To this I think the sort of array we have some understanding, if there is any "regret" later to make up for it.


http://www.bkjia.com/PHPjc/446529.html www.bkjia.com true http://www.bkjia.com/PHPjc/446529.html techarticle PHP is still more commonly used, so I studied the PHP array sorting, here to take out and share with you, hope to be useful to everyone. After understanding the Usort custom sort, we then ...

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