Sort Array in Descending Order – rsort() Function
The rsort() function sorts an array in descending order by value. This is useful when you want to organize elements from largest to smallest.
Syntax
rsort(array &$array, int $sort_flags = SORT_REGULAR): bool
Example
<?php
$numbers = [5, 2, 8, 1, 9];
rsort($numbers);
print_r($numbers);
?>

You can also try this code with Online PHP Compiler
Run Code
Output
Array
(
[0] => 9
[1] => 8
[2] => 5
[3] => 2
[4] => 1
)
Sort Array in Ascending Order (Array Values) – asort() Function
The asort() function maintains the index association while sorting the array values in ascending order. This is helpful when you need to retain the keys along with their respective values.
Syntax
asort(array &$array, int $sort_flags = SORT_REGULAR): bool
Example
<?php
$ages = ["John" => 25, "Alice" => 22, "Bob" => 30];
asort($ages);
print_r($ages);
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[Alice] => 22
[John] => 25
[Bob] => 30
)
Sort Array in Descending Order (Array Values) – arsort() Function
The arsort() function sorts an array by values in descending order while maintaining the index association.
Syntax
arsort(array &$array, int $sort_flags = SORT_REGULAR): bool
Example
<?php
$ages = ["John" => 25, "Alice" => 22, "Bob" => 30];
arsort($ages);
print_r($ages);
?>

You can also try this code with Online PHP Compiler
Run Code
Output
Array
(
[Bob] => 30
[John] => 25
[Alice] => 22
)
Sort Array in Ascending Order (Array Key) – ksort() Function
The ksort() function sorts an array by its keys in ascending order. It is often used when working with associative arrays where the keys matter.
Syntax
ksort(array &$array, int $sort_flags = SORT_REGULAR): bool
Example
<?php
$grades = ["Zara" => 85, "Mike" => 90, "Anna" => 80];
ksort($grades);
print_r($grades);
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[Anna] => 80
[Mike] => 90
[Zara] => 85
)
Sort Array in Descending Order (Array Key) – krsort() Function
The krsort() function sorts an array by its keys in descending order, making it the reverse of ksort().
Syntax
krsort(array &$array, int $sort_flags = SORT_REGULAR): bool
Example
<?php
$grades = ["Zara" => 85, "Mike" => 90, "Anna" => 80];
krsort($grades);
print_r($grades);
?>

You can also try this code with Online PHP Compiler
Run Code
Output
Array
(
[Zara] => 85
[Mike] => 90
[Anna] => 80
)
Sort Array Using Comparison Function – uasort() Function
The uasort() function sorts an array by values using a user-defined comparison function. This is highly customizable as you can define your sorting logic.
Syntax
uasort(array &$array, callable $callback): bool
Example
<?php
$products = ["Pencil" => 10, "Notebook" => 20, "Eraser" => 5];
uasort($products, function($a, $b) {
return $a - $b;
});
print_r($products);
?>

You can also try this code with Online PHP Compiler
Run Code
Output:
Array
(
[Eraser] => 5
[Pencil] => 10
[Notebook] => 20
)
Frequently Asked Questions
What is the difference between sort() and asort()?
sort() does not preserve the key association, while asort() maintains the key-value relationship of the original array.
Can I sort an associative array by keys in descending order?
Yes, you can use the krsort() function to sort an associative array by keys in descending order.
How does the uasort() function work?
The uasort() function sorts an array using a custom comparison function defined by the user. This allows you to specify how the values should be compared.
Conclusion
Sorting arrays in PHP is quite simple with the wide range of built-in functions available. Whether sorting by value or key, in ascending or descending order, PHP has a function to suit your needs. Functions like sort(), rsort(), asort(), arsort(), ksort(), krsort(), and uasort() make it easy to organize your data effectively.