Number index array:
bool usort( array &$array, callback $cmp_function )
The usort function sorts the specified array (parameter 1) in the specified way (parameter 2).
When we want to sort a multidimensional array, each element of a multidimensional array is another array type, and how do two arrays compare sizes? This requires user customization (whether to compare by the first element of each array or...).
<?php
//Define multidimensional array
$a = array(
array("sky", "blue"),
array("apple", "red"),
array("tree", "green"));
//Customize the array comparison function and compare according to 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 result
foreach($a as $elem) {
echo "$elem[0] : $elem[1]<br />";
}
?>
The result is:
sky : blue
tree : green
apple : red
Associative array:
bool uasort(array &$array, callback $cmp_function)
bool uksort(array &$array, callback $cmp_function)
Uasort, uksort uses the same as usort, where uasort() sorts the value of the associative array, and uksort() sorts the keywords (keys) of the associative array.
<?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 result is:
Tuesday : 2 2nd
Friday : 5 5th
Sunday : 0 7th
Friday : 5 5th
Sunday : 0 7th
Tuesday : 2 2nd
bool usort( array &$array, callback $cmp_function )
The usort function sorts the specified array (parameter 1) in the specified way (parameter 2).
When we want to sort a multidimensional array, each element of a multidimensional array is another array type, and how do two arrays compare sizes? This requires user customization (whether to compare by the first element of each array or...).
Copy the codeThe code is as follows:
<?php
//Define multidimensional array
$a = array(
array("sky", "blue"),
array("apple", "red"),
array("tree", "green"));
//Customize the array comparison function and compare according to 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 result
foreach($a as $elem) {
echo "$elem[0] : $elem[1]<br />";
}
?>
The result is:
Copy the codeThe code is as follows:
sky : blue
tree : green
apple : red
Associative array:
bool uasort(array &$array, callback $cmp_function)
bool uksort(array &$array, callback $cmp_function)
Uasort, uksort uses the same as usort, where uasort() sorts the value of the associative array, and uksort() sorts the keywords (keys) of the associative array.
Copy the codeThe code is 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 result is:
Tuesday : 2 2nd
Friday : 5 5th
Sunday : 0 7th
Friday : 5 5th
Sunday : 0 7th
Tuesday : 2 2nd