In real life and in the programming world, 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 sorting PHP arrays, the first three are for one-dimensional arrays, we first say these three, the fourth sort of multidimensional arrays. In the next article, that's a bit complicated.
The first group: sort and rsort, sorted according to the order of the PHP array key values, ASC and Reverse DESC, and destroy the index relationship of the original array--actually, after the index is deleted, the index of the number starting from 0 is reset. Take a look at the routine:
<?php
$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)
}
Second output:
Array (2) {
[0]=>
Int (5)
[1]=>
Int (4)
}
We found no index of our original definition. Where did it go? To be sure that they were ruthlessly deleted, if you do not care about the original index relationship, you can use them!
The second set of functions: Asort and Arsort, these two functions are more powerful, as long as they can retain the original index relationship of the array, the example of the sort and rsort respectively with these two functions, see the results of the operation:
Array (2) {
["A"]=>
Int (1)
[0]=>
Int (2)
}
Array (2) {
[0]=>
Int (2)
["A"]=>
Int (1)
}
This is a look on the understanding, needless to say it!
The third set of PHP array sort functions: Krsort and Ksort These two different from the above two groups, the two functions are to order the key name, we can replace the function of the above example to these two, see the specific operation results, here also do not say, otherwise this article is too long to write, Fear that some brothers do not have the patience to see the focus of this article, although the focus is on the bottom!
Using custom functions to sort the PHP array, there are three functions:
Uasort uses custom functions to sort the key values of the PHP array and retains the original index relationship.
Uksort uses custom functions to sort the key names of the PHP array and retains the original index relationship.
Usort the key values of the PHP array through a custom function, deletes the original index relationship, and creates a new index from scratch.
This place certainly needs an example:
<!-- p //First Top one function, this The function needs to accept two parameters, and the return value is a certain ///The first argument equals the second parameter and returns 0 when less than-1, greater than the return 1 function CMP ( $a , $b ) { $a + 1 ; $b + = 3 ; //Change these values to compare if ( $a = = $b ) return 0 ; return ( $a n Lt ; $b ) ? - 1 : 1
; } $a = array ( 1 , 4 , 3 , 5 ) ; Uasort ( $a , ' cmp ' )
; Var_dump ( $a )
; ; |
Output results:
Array (4) {
[0]=>
Int (1)
[3]=>
Int (5)
[1]=>
Int (4)
[2]=>
Int (3)
}
Oh...... Is it more messy than not to order? We are here to demonstrate the use of the method only, when you use the time to do it yourself! If you do not compare these values, for example, we are here:
$a +=1;
$b +=3;//Change these values to compare
So you might as well just use sort, right?
The other two and this is almost not said, and then the field will be the PHP for multidimensional arrays to do some experiments, and then the results and process to tell you, wait!