PHP one-dimensional array sorting method

Source: Internet
Author: User

In real life or in the world of programs, order is always important-I can't imagine what a world without order will look like! PHP array is no exception. PHP provides four groups of sorting functions for PHP arrays. The first three are for one-dimensional arrays. let's first talk about these three types. The fourth sorting method for multi-dimensional arrays is described in the next article, that is a little complicated.

Group 1: sort and rsort, which are sorted by asc and desc in the order of PHP array key values, at the same time, the index relationship of the original array is damaged-in fact, after the index is deleted, the number index starting from 0 is re-created. Take a look at the routine:

<?php$a = array("a"=>1,2);sort($a);var_dump($a); rsort($a);var_dump($a);?>

Let's take a look at the first output result, and the first output:

Array (2 ){

[0] =>

Int (1)

[1] =>

Int (2)

}

Second output:

Array (2 ){

[0] =>

Int (5)

[1] =>

Int (4)

}

Where did we find that index a was not defined? Where are you going? It is certain that they have been relentlessly deleted. If you don't care about the original index relationship, you can use them!

The second group of functions: asort and arsort. These two functions are more powerful. As long as they can retain the original index relationship of the array, replace the sort and rsort functions in the above example with the two functions respectively, view the running result:

Array (2 ){

["A"] =>

Int (1)

[0] =>

Int (2)

}

Array (2 ){

[0] =>

Int (2)

["A"] =>

Int (1)

}

You can understand it at a glance!

The third group of PHP array sorting functions: krsort and ksort are different from the above two groups. These two functions sort key names. You can replace the functions in the above example with the two, let's take a look at the specific running results. Otherwise, this article will be written too long. I'm afraid some of my brothers will not be patient enough to see the focus of this article, although the focus is below!

Sort PHP arrays by using custom functions. The following three functions are available:

Uasort sorts the key values of the PHP Array Using a custom function and retains the original index relationship.

Uksort sorts the key names of PHP arrays by using a custom function and retains the original index relationship.

Usort sorts the key values of the PHP array through a user-defined function, deletes the original index relationship, and creates a new index from scratch.

An example is required:

<! -- P // first, a function must take two parameters and return a certain value. // if the first parameter is equal to the second parameter, 0 is returned, -1 is returned when the value is less than. If the value is greater than 1, function cmp ($ a, $ B) {$ a + = 1; $ B + = 3 is returned; // after changing these values, compare if ($ a = $ B) return 0; return ($ a & lt; $ B )? -;} $ A = array (,); uasort ($ a, 'cmp '); var_dump ($ a); -->

Output result:

Array (4 ){

[0] =>

Int (1)

[3] =>

Int (5)

[1] =>

Int (4)

[2] =>

Int (3)

}

Haha ...... Is it worse than not sorting? Here we only demonstrate the method used. Let's do it yourself when you use it! If you do not compare these values, for example, here:

$ A + = 1;

$ B + = 3; // change these values and compare them.

So you might as well use sort directly, right?

The other two will not talk about this. Then, the battlefield will conduct some experiments on PHP's sorting of multi-dimensional arrays, and then tell you the results and processes. Please wait!

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.