PHP array sort is an action to sort array elements in alphabetical or numerical order, descending or ascending in PHP. Some sort based on the array keys, whereas others by the values. in this tutorial, we are going to discuss how to sort PHP arrays by value and key in ascending and descending order.
PHP array sort content
- Array sort functions
- Sort array in ascending order based on the value
- Sort array in descending order based on the value
- Sort array in ascending order based on the key
- Sort array in descending order based on the key
- The user-defined array sort function
- PHP array sort based on the local language
Array sort functions
You can easily sort an array by the sort functions. Returns true if the sorting is successful, otherwise false. By default, most of these functions sort the array in ascending order.
There are several ways to sort an array in PHP. The most common way is to use the sort()
function. This function sorts the array in ascending order, meaning that the lowest values will be at the beginning of the array. You can also use the rsort()
function to sort the array in descending order, meaning that the highest values will be at the beginning of the array.
Other sorting functions include ksort()
, which sorts the array by the keys, and asort()
, which sorts the array by the values. You can also use the usort()
function to sort the array using a custom comparison function.
- a: Sorting with key retention
- k: Sort by key
- r: Descending sort
- u: Sort by user function
Here are the functions
sort()
: sort array by value in ascending orderasort()
: sort array by value in ascending orderarsort()
: sort array by value in descending orderksort()
: sort array by key in ascending orderkrsort()
: sort array by key in descending orderrsort()
: sort array by value in descending orderuasort()
: sort array by user-defined function in ascending orderusort()
: sort array by user-defined function in ascending order
Sort the array in ascending order based on the value
PHP array sort by value can be done by sort()
and asort()
function in ascending order.
sort() function
$cars = array('fer' => "Ferrari", 'ben' => "Benz", 'bmw' => "BMW", 'vol' => "Volvo");
sort($cars);
var_dump($cars);
Output
array (size=4)
0 => string 'BMW' (length=3)
1 => string 'Benz' (length=4)
2 => string 'Ferrari' (length=7)
3 => string 'Volvo' (length=5)
asort() function
The difference between this function and the sort()
function is in the returned array keys. In the sort()
function the returned keys are indexed and in the asort()
function is the keys itself.
$cars = array('fer' => "Ferrari", 'ben' => "Benz", 'bmw' => "BMW", 'vol' => "Volvo");
asort($cars);
var_dump($cars);
Output
array (size=4)
'bmw' => string 'BMW' (length=3)
'ben' => string 'Benz' (length=4)
'fer' => string 'Ferrari' (length=7)
'vol' => string 'Volvo' (length=5)
Sort the array in descending order based on the value
PHP array sort by value can be done by rsort()
and arsort()
function in descending order.
rsort() function
$cars = array('fer' => "Ferrari", 'ben' => "Benz", 'bmw' => "BMW", 'vol' => "Volvo");
rsort($cars);
var_dump($cars);
Output
array (size=4)
0 => string 'Volvo' (length=5)
1 => string 'Ferrari' (length=7)
2 => string 'Benz' (length=4)
3 => string 'BMW' (length=3)
arsort() function
The difference between this function and the rsort()
function is in the returned array keys. In the rsort()
function the returned keys are indexed and in the arsort()
function is the keys itself.
$cars = array('fer' => "Ferrari", 'ben' => "Benz", 'bmw' => "BMW", 'vol' => "Volvo");
arsort($cars);
var_dump($cars);
Output
array (size=4)
'vol' => string 'Volvo' (length=5)
'fer' => string 'Ferrari' (length=7)
'ben' => string 'Benz' (length=4)
'bmw' => string 'BMW' (length=3)
Sort the array in ascending order based on the key
PHP array sort by the key can be done by ksort()
function in ascending order.
ksort() function
$cars = array('fer' => "Ferrari", 'ben' => "Benz", 'bmw' => "BMW", 'vol' => "Volvo");
ksort($cars);
var_dump($cars);
Output
array (size=4)
'ben' => string 'Benz' (length=4)
'bmw' => string 'BMW' (length=3)
'fer' => string 'Ferrari' (length=7)
'vol' => string 'Volvo' (length=5)
Sort the array in descending order based on the key
PHP array sort by the key can be done by krsort()
function in descending order.
krsort() function
$cars = array('fer' => "Ferrari", 'ben' => "Benz", 'bmw' => "BMW", 'vol' => "Volvo");
krsort($cars);
var_dump($cars);
Output
array (size=4)
'vol' => string 'Volvo' (length=5)
'fer' => string 'Ferrari' (length=7)
'bmw' => string 'BMW' (length=3)
'ben' => string 'Benz' (length=4)
The user-defined array sort function
usort() function
function hs_custom_sort($value1, $value2)
{
return strcmp($value1, $value2);
}
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);
usort($cars, 'hs_custom_sort');
var_dump($cars);
Output
array (size=4)
0 => string 'BMW' (length=3)
1 => string 'Benz' (length=4)
2 => string 'Ferrari' (length=7)
3 => string 'Volvo' (length=5)
uasort() function
The difference between this function and the usort()
function is in the returned array keys. In the usort()
function the returned keys are indexed and in the uasort()
function is the keys itself.
function hs_custom_sort($value1, $value2)
{
return strcmp($value1, $value2);
}
$cars = array(
"fer" => "Ferrari",
"ben" => "Benz",
"bmw" => "BMW",
"vol" => "Volvo"
);
uasort($cars, 'hs_custom_sort');
var_dump($cars);
Output
array (size=4)
'bmw' => string 'BMW' (length=3)
'ben' => string 'Benz' (length=4)
'fer' => string 'Ferrari' (length=7)
'vol' => string 'Volvo' (length=5)
Array sort based on the local language
If your data contains characters other than English, PHP has a way to sort them. You can use PHP local for this.
setlocale(LC_ALL, 'sk_SK.utf8');
$words = ["ďateľ", "auto", "železo", "byt", "kocka", "dáma", "zem", "autor", "ceduľa", "čižma"];
sort($words, SORT_LOCALE_STRING);
var_dump($words);
Output
array (size=10)
0 => string 'auto' (length=4)
1 => string 'autor' (length=5)
2 => string 'byt' (length=3)
3 => string 'ceduľa' (length=7)
4 => string 'dáma' (length=5)
5 => string 'kocka' (length=5)
6 => string 'zem' (length=3)
7 => string 'čižma' (length=7)
8 => string 'ďateľ' (length=7)
9 => string 'železo' (length=7)
First of all, we set the desired local language and then sort. In this example, the Slovak language is defined.