PHP simple array Sorting implementation code _php skill

Source: Internet
Author: User
Tags arrays numeric

PHP simple Array sorting method, for everyone to learn reference.
PHP doesn't know how to compare two arrays, and you need to create a way to compare them.

initialization of a numeric index array

Copy Code code as follows:

$arr = Array (' 100 ', ' 200 ', ' 300 ');

The above code creates an array and contains 3 values. The numeric index of the PHP array starts at 0. So the value of $arr[0] is 100.
You can copy the values from one array to another by using a simple "=" number.
If you need to save a number in ascending order to an array, you can use the range () function to automatically create the array.

The following code will create an array of numbers from 1 to 10:

Copy Code code as follows:

$nums = range (1,10);

This function also has a third parameter that allows you to set the step size for the number increment. For example: Range (1,10,2) is the odd number from 1 to 10.
PHP also supports associative arrays, where you can associate variable values with keywords. Such as:

Copy Code code as follows:

$arr = Array (' One ' =>100, ' two ' =>200, ' three ' =>300);

ii. Array Ordering

1, sort () function: sorted by alphabetical or numeric order of the array value.
The sort () function is case-sensitive, and all uppercase letters precede the lowercase letters. This function also has a second parameter that can be passed sort_numeric or sort_string or sort_regular (the default). Specifying the sort function is useful, for example, when the demon compares numbers 12 and 2 o'clock, from the string point of view, 12 is less than 2, and from a digital perspective just the opposite.
2, Asort () function, Ksort () function:
If you use an associated array, you want to keep the ordering of the keywords and values consistent after sorting. We're going to use these two functions. Ksort is sorted by key (keyword), asort is the value (in ascending order).
For example, there are arrays:
$price = Array (' apple ' = 5, ' banana ' = 6, ' Litchi ' =7);
We use Ksort ($price); The result is: Litchi--7, Apple--5, Banana--6
With Asort ($price), the result is: Apple--5, Banana--6, Litchi--7
The corresponding inverse sequence is to add a ' r ' to the sort front. i.e. Rsort (), Arsort (), Krsort ()

three, the order of multidimensional arrays

PHP knows how to compare two numbers or strings, but each element of a multidimensional array is an array.
PHP doesn't know how to compare two arrays, so you need to create a way to compare them.

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.