Home  >  Article  >  Backend Development  >  PHP array sorting method?

PHP array sorting method?

王林
王林Original
2023-05-19 10:32:37389browse

In PHP, we often need to sort arrays to better manage and process data. PHP provides a variety of array sorting methods. This article will introduce these methods and their use.

  1. sort() function

The sort() function is one of the simplest array sorting methods in php. It sorts the array in ascending order. It will change the original The order of the array. The usage of the sort() function is as follows:

$array = array(5, 3, 9, 1, 6);
sort($array);
print_r($array);

The output result is:

Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 6 [4] => 9 )
  1. rsort() function

rsort() function and sort() The function does the opposite, it sorts the array in descending order. The usage of rsort() function is as follows:

$array = array(5, 3, 9, 1, 6);
rsort($array);
print_r($array);

The output result is:

Array ( [0] => 9 [1] => 6 [2] => 5 [3] => 3 [4] => 1 )
  1. asort() function

asort() function sorts the array in ascending order Sort, but it does not change the value of the array key like the sort() function. It just reorders the values ​​of the array and keeps the association with their corresponding keys. The usage of the asort() function is as follows:

$array = array("apple" => 5, "banana" => 3, "orange" => 9, "pear" => 1, "grape" => 6);
asort($array);
print_r($array);

The output result is:

Array ( [pear] => 1 [banana] => 3 [apple] => 5 [grape] => 6 [orange] => 9 )
  1. arsort() function

arsort() function and asort() The function is similar, but it sorts the array in descending order. The usage of arsort() function is as follows:

$array = array("apple" => 5, "banana" => 3, "orange" => 9, "pear" => 1, "grape" => 6);
arsort($array);
print_r($array);

The output result is:

Array ( [orange] => 9 [grape] => 6 [apple] => 5 [banana] => 3 [pear] => 1 )
  1. ksort() function

ksort() function sorts ) sorts the array in ascending order. The usage of ksort() function is as follows:

$array = array("apple" => 5, "banana" => 3, "orange" => 9, "pear" => 1, "grape" => 6);
ksort($array);
print_r($array);

The output result is:

Array ( [apple] => 5 [banana] => 3 [grape] => 6 [orange] => 9 [pear] => 1 )
  1. krsort() function

krsort() function and ksort() The function is similar, but it sorts the array in descending order by key. The usage of krsort() function is as follows:

$array = array("apple" => 5, "banana" => 3, "orange" => 9, "pear" => 1, "grape" => 6);
krsort($array);
print_r($array);

The output result is:

Array ( [pear] => 1 [orange] => 9 [grape] => 6 [banana] => 3 [apple] => 5 )
  1. uasort() function

uasort() function provides a Custom sorting method. It allows you to define an own comparison function that will be used to sort the array. The usage of the uasort() function is as follows:

function cmp($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

$array = array("apple" => 5, "banana" => 3, "orange" => 9, "pear" => 1, "grape" => 6);
uasort($array, "cmp");
print_r($array);

The output result is:

Array ( [pear] => 1 [banana] => 3 [apple] => 5 [grape] => 6 [orange] => 9 )

In the above example, the cmp() function is used to compare the values ​​of the array and return based on the comparison result - 1, 0 or 1. The uasort() function uses this function to sort an array.

  1. usort() function

usort() function is very similar to uasort() function and also allows you to define your own comparison function that will be used to compare arrays Sort. The difference is that it does not maintain ordering of identical values. The usage of the usort() function is as follows:

function cmp($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

$array = array(5, 3, 9, 1, 6);
usort($array, "cmp");
print_r($array);

The output result is:

Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 6 [4] => 9 )

In the above example, the cmp() function is used to compare the values ​​of the array and return based on the comparison result - 1, 0 or 1. The usort() function uses this function to sort the array.

Summary

The above are common array sorting methods in PHP. They can perform various sorting operations on arrays according to our needs. It is very important to understand the use of these methods because array handling is a very important part of PHP programming. We need to choose the appropriate function to operate on the array according to our needs in order to better process and manage the data.

The above is the detailed content of PHP array sorting method?. 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