Home > Backend Development > PHP Problem > How to sort an array in php

How to sort an array in php

PHPz
Release: 2023-04-17 17:18:27
Original
534 people have browsed it

PHP is a widely used open source scripting language mainly used for web development. When developing web applications using PHP, you often need to sort data. In PHP, you can use functions such as sort(), rsort(), asort(), arsort(), ksort(), krsort(), usort(), uasort(), uksort() and other functions to sort arrays. The usage of these functions is introduced below.

  1. sort() function

The sort() function is a function in PHP used to sort an array in ascending order. This function can sort numeric arrays and string arrays, but it can only sort arrays of a single dimension. The code is as follows:

$numbers = array(4, 2, 8, 6);
sort($numbers);
print_r($numbers); // 输出:Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 )
Copy after login

In the above example, the sort() function sorts the numeric array $numbers in ascending order and outputs the result.

  1. rsort() function

rsort() function is a function in PHP used to sort an array in descending order. This function is similar to the sort() function, except that the sort order is changed to descending order. The code is as follows:

$numbers = array(4, 2, 8, 6);
rsort($numbers);
print_r($numbers); // 输出:Array ( [0] => 8 [1] => 6 [2] => 4 [3] => 2 )
Copy after login

In the above example, the rsort() function sorts the numeric array $numbers in descending order and outputs the result.

  1. asort() function

asort() function is a function in PHP used to sort arrays in ascending order. It is different from sort() function. asort() function The key values ​​in the original array will be retained. The code is as follows:

$numbers = array("d"=>4, "b"=>2, "c"=>8, "a"=>6);
asort($numbers);
print_r($numbers); // 输出:Array ( [b] => 2 [d] => 4 [a] => 6 [c] => 8 )
Copy after login

In the above example, the asort() function sorts the string array $numbers in ascending order and retains the key values ​​in the original array.

  1. arsort() function

The arsort() function is a function in PHP used to sort arrays in descending order. It is similar to the asort() function and will also retain the original The key value in the array. The code is as follows:

$numbers = array("d"=>4, "b"=>2, "c"=>8, "a"=>6);
arsort($numbers);
print_r($numbers); // 输出:Array ( [c] => 8 [a] => 6 [d] => 4 [b] => 2 )
Copy after login

In the above example, the arsort() function sorts the string array $numbers in descending order and retains the key values ​​in the original array.

  1. ksort() function

The ksort() function is a function in PHP used to sort an array in ascending order by key name. This function can only sort arrays of a single dimension. The code is as follows:

$numbers = array("d"=>4, "b"=>2, "c"=>8, "a"=>6);
ksort($numbers);
print_r($numbers); // 输出:Array ( [a] => 6 [b] => 2 [c] => 8 [d] => 4 )
Copy after login

In the above example, the ksort() function sorts the string array $numbers in ascending order by key name.

  1. krsort() function

The krsort() function is a function in PHP used to sort an array in descending order by key name. This function is similar to the ksort() function, except that the sort order is changed to descending order. The code is as follows:

$numbers = array("d"=>4, "b"=>2, "c"=>8, "a"=>6);
krsort($numbers);
print_r($numbers); // 输出:Array ( [d] => 4 [c] => 8 [b] => 2 [a] => 6 )
Copy after login

In the above example, the krsort() function sorts the string array $numbers in descending order by key name.

  1. usort() function

usort() function is a function in PHP for custom sorting of arrays. This function needs to customize a comparison function for sorting. The comparison function returns an integer value representing the comparison result. If the first argument is less than the second argument, a negative number is returned; if the first argument is equal to the second argument, zero is returned; if the first argument is greater than the second argument, a positive number is returned. The code is as follows:

function compare($a, $b) {
    if($a<$b)
        return -1;
    elseif($a==$b)
        return 0;
    else
        return 1;
}
$numbers = array(4, 2, 8, 6);
usort($numbers, "compare");
print_r($numbers); // 输出:Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 )
Copy after login

In the above example, the usort() function performs a custom ascending sorting of the numeric array $numbers, and the comparison function is the compare() function.

  1. uasort() function

The uasort() function is a function used in PHP to customize array sorting. Unlike the usort() function, uasort () function will retain the key values ​​​​in the original array. The code is as follows:

function compare($a, $b) {
    if($a<$b)
        return -1;
    elseif($a==$b)
        return 0;
    else
        return 1;
}
$numbers = array("d"=>4, "b"=>2, "c"=>8, "a"=>6);
uasort($numbers, "compare");
print_r($numbers); // 输出:Array ( [b] => 2 [d] => 4 [a] => 6 [c] => 8 )
Copy after login

In the above example, the uasort() function performs a custom ascending sort on the string array $numbers and retains the key values ​​in the original array.

  1. uksort() function

uksort() function is a function in PHP used to customize the sorting of arrays by key name. This function needs to customize a comparison function for sorting. The comparison function returns an integer value representing the comparison result. The code is as follows:

function compare($a, $b) {
    if($a<$b)
        return -1;
    elseif($a==$b)
        return 0;
    else
        return 1;
}
$numbers = array("d"=>4, "b"=>2, "c"=>8, "a"=>6);
uksort($numbers, "compare");
print_r($numbers); // 输出:Array ( [a] => 6 [b] => 2 [c] => 8 [d] => 4 )
Copy after login

In the above example, the uksort() function performs a custom ascending sort on the string array $numbers by key name.

Summary

In PHP, there are a variety of functions that can sort arrays. Developers can choose the appropriate function to sort according to their needs. If you need to customize the sorting of the array, you can use the usort(), uasort(), uksort() functions, and customize a comparison function for sorting. If you need to sort the array by key name, you can use the ksort(), krsort(), and uksort() functions. If you want to retain the key values ​​in the original array, you can use the asort(), arsort(), and uasort() functions.

The above is the detailed content of How to sort an array in php. 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