Array of numeric indices:
BOOL Usort (array & $array, callback $cmp _function)
The Usort function sorts the specified array (parameter 1) by the specified method (parameter 2).
When we want to sort a multidimensional array, each element of a multidimensional array is an array type, and how do two arrays compare size? This is required for user customization (whether the first element of each array is compared or ...).
Copy Code code as follows:
<?php
Defining multidimensional arrays
$a = Array (
Array ("Sky", "Blue"),
Array ("Apple", "Red"),
Array ("Tree", "green"));
A custom array comparison function that is compared by the second element of the array.
function My_compare ($a, $b) {
if ($a [1] < $b [1])
return-1;
else if ($a [1] = = $b [1])
return 0;
Else
return 1;
}
Sort
Usort ($a, ' my_compare ');
Output results
foreach ($a as $elem) {
echo "$elem [0]: $elem [1]<br/>];
}
?>
The results are:
Copy Code code as follows:
Sky:blue
Tree:green
Apple:red
Associative arrays:
BOOL Uasort (array & $array, callback $cmp _function)
BOOL Uksort (array & $array, callback $cmp _function)
Uasort, the Uksort usage is the same as Usort, where Uasort () sorts the value (value) of the associative array, and uksort () sorts the keyword (key) of the associative array.
Copy Code code as follows:
<?php
$a = Array (
' Sunday ' => array (0, ' 7th '),
' Friday ' => array (5, ' 5th '),
' Tuesday ' => array (2, ' 2nd '));
function My_compare ($a, $b) {
if ($a [1] < $b [1])
return-1;
else if ($a [1] = = $b [1])
return 0;
Else
return 1;
}
Sort by the second element (7th,5th,2nd) of the value of the $a array
Uasort ($a, ' my_compare ');
foreach ($a as $key => $value) {
echo "$key: $value [0] $value [1]<br/>];
}
Sort by the second character (r,u,u) of the keyword in the $a array
Uksort ($a, ' my_compare ');
foreach ($a as $key => $value) {
echo "$key: $value [0] $value [1]<br/>];
}
?>
The results are:
Tuesday:2 2nd
Friday:5 5th
sunday:0 7th
Friday:5 5th
sunday:0 7th
Tuesday:2 2nd