Home  >  Article  >  Backend Development  >  Let’s talk about the built-in sorting method of PHP arrays

Let’s talk about the built-in sorting method of PHP arrays

PHPz
PHPzOriginal
2023-04-19 09:22:22408browse

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.

  1. sort()

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:

  • SORT_REGULAR - the default sorting rule, each value is compared in the usual way.
  • SORT_NUMERIC - Sort by numerical value.
  • SORT_STRING - Sort strings lexicographically.
  • SORT_LOCALE_STRING - Sort strings lexicographically according to the current localization settings.
  • SORT_NATURAL - Sort by natural number sequence. For example, "a1" comes before "10".
  • SORT_FLAG_CASE - Can be used with any of the previous collation rules to treat the upper and lower case of string letters as different.

Example:

$arr ​​= array("apple", "banana", "grape");
sort($arr);
print_r($ arr);

Output result:

Array ( [0] => apple [1] => banana [2] => grape )

  1. rsort()

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 )

  1. asort()

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 )

  1. arsort()

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 )

  1. ksort()

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 )

  1. krsort()

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 )

  1. usort()

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 )

  1. uasort()

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 )

  1. uksort()

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn