Array is used as the parameter of the sort function, and after sorting, the array itself is changed, and the return value of the function is bool type.
2 function name appears single A association, meaning, in the process of ordering by value, maintain the key=>value of the corresponding relationship is unchanged
3 function name in a single K represents key, meaning, in the process of sorting by value according to the array key instead of the value of the array sort
4 The expression reverse of a single r in the name of a function, in the opposite order of not adding r
In the 5 function name, a single U representation user-defined, meaning, is sorted using a user-defined function, and if the logic of the function is the argument 1< parameter 2 returns a negative number, it is sorted in ascending order (P1 small 2 to negative liters).
--------------------sort function in ascending order--------------------------------
The code is as follows |
Copy Code |
BOOL Sort (array & $array [, int $sort _flags= sort_regular]) <?php $fruits = Array ("Lemon", "orange", "banana", "apple"); Sort ($fruits); Var_dump ($fruits); ?> Results: Array 0 => string ' apple ' (length=5) 1 => string ' banana ' (length=6) 2 => string ' Lemon ' (length=5) 3 => string ' Orange ' (length=6) |
--------------------Rsort Descending order--------------------
The code is as follows |
Copy Code |
<?php $fruits = Array ("Lemon", "orange", "banana", "apple"); Rsort ($fruits); Var_dump ($fruits); ?> Results: Array 0 => string ' orange ' (length=6) 1 => string ' Lemon ' (length=5) 2 => string ' banana ' (length=6) 3 => string ' Apple ' (length=5) |
---------------Asort in ascending order of two-dimensional array values (keep Key=>value associated)-----------
The code is as follows |
Copy Code |
<?php $fruits = Array ("D" => "Lemon", "a" => "Orange", "B" => "banana", "C" => "Apple"); Asort ($fruits); Var_dump ($fruits); ?> Results: Array ' C ' => string ' apple ' (length=5) ' B ' => string ' banana ' (length=6) ' d ' => string ' lemon ' (length=5) ' A ' => string ' orange ' (length=6) |
--------------Arsort in descending order of two-dimensional array values (keep Key=>value associated)--------------
code is as follows |
copy code |
<?php $fruits = Array ("D" => "Lemon", "a" => "Orange", "B" => "banana", "C" => "Apple"); Arsort ($fruits); Var_dump ($fruits); ?> Results Array ' A ' => string ' orange ' (length=6) ' d ' => string ' lemon ' (length=5) ' B ' => string ' banana ' (length=6) ' C ' => string ' apple ' (length=5) --------------------Ksort is arranged in the key ascending order of the array--------------<?php$fruits = Array ("D" => "Lemon", "a" => "Orange", "b" = > "Banana", "C" => "Apple"); Ksort ($fruits); Var_dump ($fruits); ?> Results Array ' A ' => string ' orange ' (length=6) ' B ' => string ' banana ' (length=6) ' C ' => string ' apple ' (length=5) ' d ' => string ' lemon ' (length=5) |
---------------------Krsort in descending order of array key--------------------------------
The code is as follows |
Copy Code |
<?php $fruits = Array ("D" => "Lemon", "a" => "Orange", "B" => "banana", "C" => "Apple"); Krsort ($fruits); Var_dump ($fruits); ?> Array ' d ' => string ' lemon ' (length=5) ' C ' => string ' apple ' (length=5) ' B ' => string ' banana ' (length=6) ' A ' => string ' orange ' (length=6) |
----------------Usort functions are sorted by user-defined functions----------------
The code is as follows |
Copy Code |
<?php function cmp ($a, $b) { if ($a = = $b) { return 0; } Return ($a < $b)? -1:1; } $a = Array (3, 2, 5, 6, 1); Usort ($a, "CMP"); Var_dump ($a); ?> Results: Array 0 => Int 1 1 => int 2 2 => int 3 3 => int 5 4 => int 6 |
-----------------Uksort uses custom functions to sort by the key of the array-----------------
The code is as follows |
Copy Code |
<?php function cmp ($a, $b) { $a = preg_replace (' @^ (a|an|the) @ ', ', $a); $b = Preg_replace (' @^ (a|an|the) @ ', ', $b); Return strcasecmp ($a, $b); } $a = Array ("John" => 1, "The Earth" => 2, "An Apple" => 3, "a Banana" => 4); Uksort ($a, "CMP"); Var_dump ($a); ?> Results: Array ' An apple ' => int 3 ' A banana ' => int 4 ' The earth ' => int 2 ' John ' => int 1 |
-------------------Uasort the array with the custom function to sort by value, keeping the index relationship unchanged---------
code is as follows |
copy code |
<?php Comparison function function cmp ($a, $b) { if ($a = = $b) { return 0; } Return ($a < $b)? -1:1; } Array to is sorted $array = Array (' A ' => 4, ' B ' => 8, ' C ' =>-1, ' d ' =>-9, ' E ' => 2, ' F ' => 5, ' G ' => 3, ' H ' =>-4); Var_dump ($array); Sort and print the resulting array Uasort ($array, ' CMP '); Var_dump ($array); ?> Results: Array ' A ' => int 4 ' B ' => int 8 ' C ' => int-1 ' d ' => int-9 ' E ' => int 2 ' F ' => int 5 ' G ' => int 3 ' H ' => int-4 Array ' d ' => int-9 ' H ' => int-4 ' C ' => int-1 ' E ' => int 2 ' G ' => int 3 ' A ' => int 4 ' F ' => int 5 ' B ' => int 8 |
-------------------Array_multisort to sort multiple arrays or multidimensional arrays---------
code is as follows |
copy code |
<?php $ar = Array ( Array ("Ten", "One,", "a"), Array (1, 2, 2, 3, 1) ); Array_multisort ($ar [0], SORT_ASC, sort_string, $ar [1], sort_numeric, sort_desc); Var_dump ($ar); ?> Result: Array 0 => Array 0 => string ' (length=2) 1 => int 2 => int 3 => int 4 => string ' A ' (length=1) 1 => Array 0 => int 1 |
1 => int 3
2 => string ' 2 ' (length=1)
3 => int 2
4 => int 1
Description
1 in the example above: $ar array precedence follows the string value of $ar[0] in ascending order, if the string values are equal, and then sorted by the numeric values in the $ar[1] array.
2 The parameter of any position of the Array_multisort function, if it is an array, represents the value used for the sort.
If you have more than one array parameter, precedence is sorted by the preceding array value, if it is a constant, for example
SORT_ASC, Sort_desc, Sort_regular,sort_numeric, sort_string.
Represents a sort method (precedence before array values).