Can php implement array sorting method?

PHPz
Release: 2023-04-18 09:17:06
Original
281 people have browsed it

In PHP, many array sorting functions are provided, which can realize various sorting methods of arrays. Here are some commonly used array sorting methods.

  1. sort(), rsort() function

The sort() function is used to sort the array in ascending order, while the rsort() function is used to sort the array in descending order. Both functions sort the original array and do not create a new array.

For example:

$arr = array(1, 5, 2, 8, 3); 
sort($arr); // 升序排序
print_r($arr); // 输出:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 5 [4] => 8 )
 
rsort($arr); // 降序排序
print_r($arr); // 输出:Array ( [0] => 8 [1] => 5 [2] => 3 [3] => 2 [4] => 1 )
Copy after login
  1. asort(), arsort() function

asort() function is used to sort the array in ascending order, and is the same as sort( )similar. But unlike sort(), asort() also retains array key names. Similarly, arsort() preserves key names in descending order. These two functions also sort the original array.

For example:

$arr = array("a" => 5, "b" => 3, "c" => 8, "d" => 2);
asort($arr); // 升序排序并保留键名
print_r($arr); // 输出:Array ( [d] => 2 [b] => 3 [a] => 5 [c] => 8 )
 
arsort($arr); // 降序排序并保留键名
print_r($arr); // 输出:Array ( [c] => 8 [a] => 5 [b] => 3 [d] => 2 )
Copy after login
  1. ksort(), krsort() function

Different from the above two functions, ksort() and krsort() functions Is to sort the array according to the key name. ksort() sorts in ascending order, krsort() sorts in descending order.

For example:

$arr = array("a" => 5, "c" => 8, "b" => 3, "d" => 2);
ksort($arr); // 按照键名升序排序
print_r($arr); // 输出:Array ( [a] => 5 [b] => 3 [c] => 8 [d] => 2 )
 
krsort($arr); // 按照键名降序排序
print_r($arr); // 输出:Array ( [d] => 2 [c] => 8 [b] => 3 [a] => 5 )
Copy after login
  1. usort() function

If you need to use a custom algorithm to sort the array, you can use the usort() function. This function requires a function as argument that compares the size of array elements. When array elements need to be swapped, this function automatically swaps them.

For example:

$arr = array("apple", "banana", "peach", "orange");
function cmp($a, $b) {
    return strlen($a) - strlen($b);
}
usort($arr, "cmp");
print_r($arr); // 输出:Array ( [0] => apple [1] => peach [2] => banana [3] => orange )
Copy after login

The above example uses a custom algorithm to sort in ascending order according to the length of the string.

Summary

The above are the commonly used array sorting functions in PHP. You need to use it according to the actual needs of your own program. Of course, custom algorithms can also be combined to meet more complex needs.

The above is the detailed content of Can php implement array sorting method?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!