Home >Backend Development >PHP Problem >Let's talk about the built-in sorting method of PHP arrays
In PHP, array is a very commonly used data type. For array processing, sorting is a very important operation. In PHP, there are many built-in sorting functions. This article will introduce the built-in sorting method of PHP array.
The sort() function is used to sort an array in ascending order. The specific syntax is:
sort(array &$array [, int $sort_flags = SORT_REGULAR]): bool
Among them, $array represents the array that needs to be sorted, and $sort_flags represents the sorting rule. By default, sorting is in ascending order. The $sort_flags optional parameter has the following types:
Example:
$arr = array("apple", "banana", "grape");
sort($arr);
print_r($ arr);
Output result:
Array ( [0] => apple [1] => banana [2] => grape )
The rsort() function is very similar to the sort() function, except that it sorts the array in descending order. The specific syntax is:
rsort(array &$array [, int $sort_flags = SORT_REGULAR]): bool
Example:
$arr = array("apple", "banana", "grape");
rsort($arr);
print_r($arr);
Output result:
Array ( [0] => grape [1] => banana [2] => apple )
asort() function sorts the array in ascending order and retains The original key name. The specific syntax is:
asort(array &$array [, int $sort_flags = SORT_REGULAR]): bool
Example:
$arr = array("b" = > 2, "a" => 1, "c" => 3);
asort($arr);
print_r($arr);
Output result:
Array ( [a] => 1 [b] => 2 [c] => 3 )
arsort The () function is very similar to the asort() function, except that it sorts the array in descending order. The specific syntax is:
arsort(array &$array [, int $sort_flags = SORT_REGULAR]): bool
Example:
$arr = array("b" = > 2, "a" => 1, "c" => 3);
arsort($arr);
print_r($arr);
Output result:
Array ([c] => 3 [b] => 2 [a] => 1 )
ksort () function sorts the array in ascending order by key name. The specific syntax is:
ksort(array &$array [, int $sort_flags = SORT_REGULAR]): bool
Example:
$arr = array("b" = > 2, "a" => 1, "c" => 3);
ksort($arr);
print_r($arr);
Output result:
Array ( [a] => 1 [b] => 2 [c] => 3 )
krsort The () function is very similar to the ksort() function, except that it sorts the array in descending order of key names. The specific syntax is:
krsort(array &$array [, int $sort_flags = SORT_REGULAR]): bool
Example:
$arr = array("b" = > 2, "a" => 1, "c" => 3);
krsort($arr);
print_r($arr);
Output result:
Array ([c] => 3 [b] => 2 [a] => 1 )
usort The () function is used to customize the sorting of the array, that is, sorting according to the rules defined by yourself. The specific syntax is:
usort(array &$array, callable $function): bool
Among them, $array represents the array that needs to be sorted, and $function represents the function used to compare array elements. The function needs to return an integer when comparing, representing the comparison result of the two values.
Example:
$arr = array(3, 5, 1, 4, 2);
usort($arr, function($a, $b) {
if ($a == $b) return 0; return ($a < $b) ? -1 : 1;
});
print_r($arr);
Output result:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
uasort() function is very similar to usort() function Similar, except it retains the original key names. The specific syntax is:
uasort(array &$array, callable $function): bool
Example:
$arr = array("b" => 2, "a" => 1, "c" => 3);
uasort($arr, function($a, $b) {
if ($a == $b) return 0; return ($a < $b) ? -1 : 1;
});
print_r($ arr);
Output result:
Array ( [a] => 1 [b] => 2 [c] => 3 )
uksort() function sorts the key names of the array according to custom rules. The specific syntax is:
uksort(array &$array, callable $function): bool
Among them, $array represents the array that needs to be sorted, and $function represents the function used to compare array key names. The function needs to return an integer when comparing, representing the comparison result of the two key names.
Example:
$arr = array("b" => 2, "a" => 1, "c" => 3);
uksort($ arr, function($a, $b) {
if ($a == $b) return 0; return ($a < $b) ? -1 : 1;
});
print_r($arr);
Output result:
Array ([a] => 1 [b] => 2 [c] => 3 )
Summary:
In PHP, there are a variety of sorting functions to choose from. According to different needs, choosing different sorting methods can allow us to operate arrays more efficiently.
The above is the detailed content of Let's talk about the built-in sorting method of PHP arrays. For more information, please follow other related articles on the PHP Chinese website!