Php simple array sorting method

Source: Internet
Author: User
Php simple array sorting method

  1. $ Arr = array ('20140901', '20160901', '20160901 ');

The code above creates an array with three values. The numeric index of the PHP array starts from 0. Therefore, the value of $ arr [0] is 100. You can use a simple "=" to copy the values in an array to another array. To save numbers in ascending order to an array, you can use the range () function to automatically create this array. The following code creates an array of numbers from 1 to 10:

  1. $ Nums = range (1, 10 );

This function has a third parameter, which can be used to set the step size for numerical growth. For example, range (1, 10, 2) is an odd number between 1 and 10. PHP also supports related arrays, that is, you can associate variable values with keywords. For example:

  1. $ Arr = array ('one' => 100, 'two' => 200, 'Three '=> 300 );

II,Array sorting1. sort () function: sort by letters or numbers of array values in ascending order. The sort () function is case-sensitive. all uppercase letters are in front of lowercase letters. This function also has a second parameter, which can be passed SORT_NUMERIC, SORT_STRING or SORT_REGULAR (default ). It is very useful to specify the sorting function. for example, when the number 12 and 2 are compared to the number 12, the number 12 is smaller than 2, and the number is the opposite.

2. asort () function, ksort () function: if related arrays are used, the sorting of keywords and values must be consistent after sorting. These two functions are required. Ksort is sort by key (keyword), and asort is sort by value (by value) (both in ascending order ). For example, there is an array: $ price = array ('apple' = 5, 'banana '= 6, 'lychee' = 7); we use ksort ($ price ); the sorting result is: lychee -- 7, Apple -- 5, bananas -- 6 use asort ($ price); the sorting result is: Apple -- 5, bananas -- 6, the reverse sorting corresponding to Litchi -- 7 is to add an 'R' before sort '. That is, rsort (), arsort (), krsort ()

3. multi-dimensional array sortingPHP knows how to compare two numbers or strings, but each element of a multi-dimensional array is an array. PHP does not know how to compare two arrays, so it is necessary to establish a method to compare them.

>>> For more information, see The php array sorting 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.