Table of contents
1.
Introduction
2.
Sort Array in Ascending Order – sort() Function
2.1.
Syntax
2.2.
Example
3.
Sort Array in Descending Order – rsort() Function
3.1.
Syntax
3.2.
Example
4.
Sort Array in Ascending Order (Array Values) – asort() Function
4.1.
Syntax
4.2.
Example
5.
Sort Array in Descending Order (Array Values) – arsort() Function
5.1.
Syntax
5.2.
Example
6.
Sort Array in Ascending Order (Array Key) – ksort() Function
6.1.
Syntax
6.2.
Example
7.
Sort Array in Descending Order (Array Key) – krsort() Function
7.1.
Syntax
7.2.
Example
8.
Sort Array Using Comparison Function – uasort() Function
8.1.
Syntax
8.2.
Example
9.
Frequently Asked Questions
9.1.
What is the difference between sort() and asort()?
9.2.
Can I sort an associative array by keys in descending order?
9.3.
How does the uasort() function work?
10.
Conclusion
Last Updated: Jan 19, 2025
Easy

Sorting Arrays in PHP

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Sorting Arrays in PHP is an essential feature that allows developers to organize data stored in arrays in a specific order, such as ascending or descending. PHP provides a variety of built-in functions like sort(), rsort(), asort(), ksort(), and usort() to handle sorting based on values or keys, maintaining indexes, or even applying custom sorting logic.

Sorting Arrays in PHP

In this article, we will discuss the commonly used PHP array sorting functions, explain their usage, and provide examples. 

Sort Array in Ascending Order – sort() Function

The sort() function sorts an array in ascending order by value. It is commonly used when you want the array elements to be rearranged from smallest to largest.

Syntax

sort(array &$array, int $sort_flags = SORT_REGULAR): bool

Example

<?php
$numbers = [5, 2, 8, 1, 9];
sort($numbers);
print_r($numbers);
?>
You can also try this code with Online PHP Compiler
Run Code


Output

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

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. 

Live masterclass