With a lot of array sorting functions in PHP, let's hit about the use of PHP array sorting.
The 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 type bool.
2 A single-a representation of association appears in the function name, meaning that the correspondence of Key=>value remains the same during sorting by value
3 A single k in the function name denotes key, meaning that it is ordered by the value of the array key instead of the value of the array during sorting by values
4 The representation of single r appears in the function name reverse, meaning, in the reverse order of not adding r
5 a single U representation appears in the function name user-defined, meaning, sort using user-defined function, if the logic of the function is parameter 1<>< p=""><>
--------------------sort function Ascending order--------------------------------
The code is as follows |
Copy Code |
BOOL Sort (array & $array [, int $sort _flags= sort_regular]) $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 in descending order--------------------
The code is as follows |
Copy Code |
$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 is arranged in ascending order of two-dimensional array values (maintains Key=>value association)-----------
The code is as follows |
Copy Code |
$fruits = Array ("d" = "lemon", "a" and "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 (maintains Key=>value association)--------------
code as follows |
copy code |
$fruits = Array ("d" = "lemon", "a" and "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 in ascending order of the array's key-------------- "Lemon", "a" and "Orange", "b" = "banana", "C" and "=" 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 the array key--------------------------------
The code is as follows |
Copy Code |
$fruits = Array ("d" = "lemon", "a" and "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 |
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 Use a custom function to sort by the key of the array-----------------
The code is as follows |
Copy Code |
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" and "4"); Uksort ($a, "CMP"); Var_dump ($a); ?> Results: Array ' An apple ' = 3 int ' a banana ' = = int 4 ' The Earth ' = + int 2 ' John ' = int 1 |
-------------------Uasort the array with a custom function to sort by value, keeping the index relationship constant---------
code as follows |
copy code |
Comparison function function cmp ($a, $b) { if ($a = = $b) { return 0; } Return ($a < $b)? -1:1; } Array to be 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---------
|
copy code |
"!--? php $ar = array ( Array ("10", One, one, 1, "a"), Array (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 one 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 above example: $ar array precedence follows the string value of $ar[0] in ascending order, if the string value is equal, and then in descending order of the numeric value of the $ar[1] array.
2 The parameter of any position of the Array_multisort function if it is an array, indicating the value to use when sorting,
If there are multiple array parameters, prioritize the array values in front of them, if they are constants, for example
SORT_ASC, Sort_desc, Sort_regular,sort_numeric, sort_string.
Represents the Sort method (the array takes precedence before the value).
http://www.bkjia.com/PHPjc/444669.html www.bkjia.com true http://www.bkjia.com/PHPjc/444669.html techarticle with a lot of array sorting functions in PHP, let's hit about the use of PHP array sorting. The array is used as the parameter of the sort function, after the array itself is sorted ...