PHP one-dimensional array sorting method _php Tutorial

Source: Internet
Author: User
In real life and in the world of procedure, order is always important--I can't imagine what a world without order would look like! PHP arrays are no exception. PHP provides four sets of functions for the ordering of PHP arrays, the first three are for one-dimensional arrays, we first say that the three, the fourth kind of multidimensional array of the sort in the next article, that's a bit complicated.

The first group: sort and Rsort, sorted by the order of the PHP array key values ASC and reverse desc, while destroying the index relationship of the original array-in fact, after dropping the index, re-establishes the 0-based numeric index. Take a look at the routines:


      $a = Array("a"=1,2);Sort($a);Var_dump($a); Rsort($a);Var_dump($a);?>

Take a look at the first output, the first output:

Array (2) {

[0]=>

Int (1)

[1]=>

Int (2)

}

A second output:

Array (2) {

[0]=>

Int (5)

[1]=>

Int (4)

}

Where did we find the index a without our original definition? Where is it? To be sure, they are ruthlessly deleted, if you do not care about the original index relationship, you can use them!

The second set of functions: Asort and Arsort, the two functions are a bit more powerful, as long as they can preserve the original index of the array, the above example of the sort and rsort with the two functions, respectively, to see the results of the operation:

Array (2) {

["A"]=>

Int (1)

[0]=>

Int (2)

}

Array (2) {

[0]=>

Int (2)

["A"]=>

Int (1)

}

This one can understand, needless to say!

The third set of PHP array sorting functions: Krsort and Ksort These two are different from the above two groups, these two functions are the key name of the order, we can replace the above example of the function of these two, to see the specific operation results, here also do not say, otherwise this article is too long to write, Afraid that some brothers have no patience to see the focus of this article, although the focus is below!

Using a custom function to sort the PHP array, there are three functions:

Uasort uses a custom function to sort the key values of the PHP array and preserves the original index relationship.

Uksort uses a custom function to sort the key names of the PHP array and preserves the original index relationship.

Usort sorts the key values of the PHP array by using a custom function, and deletes the original index relationship, creating a new index from scratch.

This place certainly needs an example:


      

Output Result:

Array (4) {

[0]=>

Int (1)

[3]=>

Int (5)

[1]=>

Int (4)

[2]=>

Int (3)

}

Oh...... Is it worse than not sorting? Here we just demonstrate the way to use it, you can do it yourself when you use it! If you do not compare these values, for example, we will:

$a +=1;

$b +=3;//to compare these values after changing them

Then you might as well just use sort, wouldn't you?

Other two other than this, and then the battlefield will be the PHP for multi-dimensional array to do some experiments, and then the results and procedures to tell you, wait!

http://www.bkjia.com/PHPjc/768910.html www.bkjia.com true http://www.bkjia.com/PHPjc/768910.html techarticle in real life or in the program world, the order is always very important. I can't imagine what a world without order would look like! PHP arrays are no exception. PHP provides four groups for PHP arrays ...

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