In this tutorial, you will learn how to sort an array in PHP according to your needs.
All about PHP array sorting
- Sort an array in ascending and descending order.
- Sorting of associative arrays by value.
- PHP array sorting by keys.
- Array natural sorting.
- Multidimensional array sorting by value.
- Sort Multidimensional array by Keys.
1. Sort an array in ascending and descending order
The sort()
function is used to sort an array in ascending order values, but this function does not keep the array keys.
<?php
$names = ['Chris', 'Alex', 'David', 'Bob R. Narron'];
$num = [52, 63, 12, 29, 36];
// Ascending Order by Value
sort($names);
sort($num);
echo '<pre>';
print_r($names);
echo '<br>';
print_r($num);
echo '</pre>';
Array
(
[0] => Alex
[1] => Bob R. Narron
[2] => Chris
[3] => David
)
Array
(
[0] => 12
[1] => 29
[2] => 36
[3] => 52
[4] => 63
)
The rsort()
function is similar to the sort()
function, but this function sorts an array in descending order by values.
<?php
$names = ['Chris', 'Alex', 'David', 'Bob M. Narron'];
$num = [52, 63, 12, 29, 36];
// Descending Order by Value
rsort($names);
rsort($num);
echo '<pre>';
print_r($names);
echo '<br>';
print_r($num);
echo '</pre>';
Array
(
[0] => David
[1] => Chris
[2] => Bob M. Narron
[3] => Alex
)
Array
(
[0] => 63
[1] => 52
[2] => 36
[3] => 29
[4] => 12
)
2. Sort associative arrays in ascending and descending order by value
You can use the sort()
or rsort()
function to sort an associative array in ascending or descending order. But these functions will convert the associative array into a numeric array.
If you want to sort an associative array in ascending or descending order by value but also want to keep the array keys, use the asort()
or arsort()
function.
asort()
is for sorting by values in ascending order.arsort()
is for sorting by values in descending order.
<?php
$names = [
1 => 'Chris',
2 => 'Bob N. Narron',
3 => 'Alex',
4 => 'David'
];
$marks = [
"Mark" => 25,
"Peter" => 36,
"David" => 29,
"Ivan" => 17
];
// Ascending Order by Value
asort($names);
// Descending Order by Vlaue
arsort($marks);
echo "<pre># Ascending\n";
print_r($names);
echo "<br># Descending\n";
print_r($marks);
echo '</pre>';
# Ascending
Array
(
[3] => Alex
[2] => Bob N. Narron
[1] => Chris
[4] => David
)
# Descending
Array
(
[Peter] => 36
[David] => 29
[Mark] => 25
[Ivan] => 17
)
3. PHP sort array by keys
If you want to sort an associative array in ascending or descending order by key, use the ksort()
or krsort()
function.
ksort()
is for sorting by keys in ascending order.krsort()
is for sorting by keys in descending order.
<?php
$marks = [
"Chris" => 25,
"Alex" => 36,
"David" => 29,
"Bob" => 17
];
$names = [
2 => 'Pippin',
4 => 'Ronald N. Narron',
3 => 'David',
1 => 'Ivan'
];
// Ascending Order by Value
ksort($marks);
// Descending Order by Vlaue
krsort($names);
echo "<pre># Ascending by keys\n";
print_r($marks);
echo "<br># Descending by keys\n";
print_r($names);
echo '</pre>';
# Ascending by keys
Array
(
[Alex] => 36
[Bob] => 17
[Chris] => 25
[David] => 29
)
# Descending by keys
Array
(
[4] => Ronald N. Narron
[3] => David
[2] => Pippin
[1] => Ivan
)
4. PHP array natural sorting
You can do the natural sorting by using the natsort()
function that orders alphanumeric strings in the way a human being would while maintaining key/value associations, and the values keep their original keys.
<?php
$images_sort = ['img4.png', 'img30.png', 'img10.png', 'img2.png'];
$images_natsort = ['img4.png', 'img30.png', 'img10.png', 'img2.png'];
sort($images_sort);
natsort($images_natsort);
echo '<pre>';
echo "# sort\n";
print_r($images_sort);
echo "<br># natsort\n";
print_r($images_natsort);
echo '</pre>';
# sort
Array
(
[0] => img10.png
[1] => img2.png
[2] => img30.png
[3] => img4.png
)
# natsort
Array
(
[3] => img2.png
[0] => img4.png
[2] => img10.png
[1] => img30.png
)
5. PHP sort multidimensional array by value
If you want to sort a PHP multidimensional array order by values, you can use the usort()
function.
But, the usort()
function does not keep the array keys, and if you wish to keep the keys you can use the uasort()
function.
These two functions sort an array by values using a user-defined comparison function.
These two function takes two parameters, the first parameter is an array and the second is a callback function where you have to define the comparison logic.
usort($array, function(mixed $a, mixed $b){
// comparison logic
});
uasort($array, function(mixed $a, mixed $b){
// comparison logic
});
The comparison function must return an integer –
- return 0 means
$a === $b
- return 1 (a positive number) means
$a > $b
- -1 (a negative number) means
$a < $b
In the following example, a PHP multidimensional array is sorted in ascending order by the appeared year of the programming languages.
<?php
$languages = [
'b' => [
'lang' => 'c++',
'appeared' => 1985,
],
'd' => [
'lang' => 'Java',
'appeared' => 1995,
],
'a' => [
'lang' => 'Python',
'appeared' => 1991,
],
'c' => [
'lang' => 'PHP',
'appeared' => 1995,
],
'f' => [
'lang' => 'Node.js',
'appeared' => 2009,
],
'e' => [
'lang' => 'TypeScript',
'appeared' => 2012,
],
];
uasort($languages, function ($a, $b) {
return $a['appeared'] <=> $b['appeared'];
});
echo "<pre># Ascending order by Appeared\n";
print_r($languages);
echo '</pre>';
# Ascending order by Appeared
Array
(
[b] => Array
(
[lang] => c++
[appeared] => 1985
)
[a] => Array
(
[lang] => Python
[appeared] => 1991
)
[d] => Array
(
[lang] => Java
[appeared] => 1995
)
[c] => Array
(
[lang] => PHP
[appeared] => 1995
)
[f] => Array
(
[lang] => Node.js
[appeared] => 2009
)
[e] => Array
(
[lang] => TypeScript
[appeared] => 2012
)
)
If you want to sort the above multidimensional in descending order by the appeared year, you can use the following logic –
uasort($languages, function ($a, $b) {
return $b['appeared'] <=> $a['appeared'];
});
6. PHP sort a multidimensional array by Keys
With the help of the uksort()
function, you can sort a PHP multidimensional array by Keys, in the rest of the cases, this function is similar to the uasort()
function.
But you can also use the ksort()
or krsort()
function to sort a multidimensional array in ascending or descending order by keys.
The following example is with the uksort()
function –
<?php
$languages = [
'b' => [
'lang' => 'c++',
'appeared' => 1985,
],
'd' => [
'lang' => 'Java',
'appeared' => 1995,
],
'a' => [
'lang' => 'Python',
'appeared' => 1991,
],
'c' => [
'lang' => 'PHP',
'appeared' => 1995,
],
'f' => [
'lang' => 'Node.js',
'appeared' => 2009,
],
'e' => [
'lang' => 'TypeScript',
'appeared' => 2012,
],
];
uksort($languages, function ($a, $b) {
return $a <=> $b;
});
echo "<pre># Ascending order by Keys\n";
print_r($languages);
echo '</pre>';
# Ascending order by Keys
Array
(
[a] => Array
(
[lang] => Python
[appeared] => 1991
)
[b] => Array
(
[lang] => c++
[appeared] => 1985
)
[c] => Array
(
[lang] => PHP
[appeared] => 1995
)
[d] => Array
(
[lang] => Java
[appeared] => 1995
)
[e] => Array
(
[lang] => TypeScript
[appeared] => 2012
)
[f] => Array
(
[lang] => Node.js
[appeared] => 2009
)
)
Logic of sorting the above multidimensional array in descending order by keys for the uksort()
function –
uksort($languages, function ($a, $b) {
return $b <=> $a;
});