What is PHP uasort() Function?
If you want to sort an array items based on custom rules or/and based on multiple fields and also keeping existing key-value association, use uasort() function. You can perform complex sorting or multidimensional array sorting with this function.
How PHP uasort() Function Works (Sorts)?
The uasort() function has a callback function (which is a user-defined comparison function) in its second parameter. It is the heart of the uasort() function as it determines the order of the elements of the array. The array is the first parameter of the uasort() function. The comparison function also has two parameters which represents 2 elements from the array.
The comparison function returns one of the following 3 integers based on the comparison. Let’s check how it compares-
- 0: If both elements are equal, it returns 0.
- Greater than 0 (ex. 1): If 1st element is larger than the 2nd element, it returns an integer which is greater than 0.
- Less than 0 (ex.-1): If 1st element is smaller than the 2nd element, it returns an integer which is less than 0.
Syntax:
uasort(array, callback)
Parameters:
The Function has 2 required parameters –
array (Required): it specifies an array.
callback (Required): It is a comparison function.
Return Values:
The function always returns TRUE.
Examples:
Example 1:
<?php
function sortByDensity( $a, $b){
if ($a == $b)
return 0;
if ($a < $b)
return -1;
else
return 1;
}
$density = array("New Jersey" => 1291, "Massachusetts" => 914, "Connecticut" => 758, "Maryland" => 645, "Delaware" => 539);
usort($density, "sortByDensity");
print_r($density);
?>
</pre>
Output:
Array
(
[Delaware] => 539
[Maryland] => 645
[Connecticut] => 758
[Massachusetts] => 914
[New Jersey] => 1291
)
Example 2: Sorting a multidimensional array-
<pre>
<?php
function sortByPopulation( $a, $b){
if ($a["Population"] == $b["Population"])
return 0;
if ($a["Population"] > $b["Population"])
return 1;
else
return -1;
}
$density = [
"New Jersey" => ["Density" => 1291, "Population" => 9500000],
"Massachusetts" => ["Density" => 914, "Population" => 7100000],
"Connecticut" => ["Density" => 758, "Population" => 3600000],
"Maryland" => ["Density" => 645, "Population" => 6200000],
"Delaware" => ["Density" => 539, "Population" => 1000000]
];
usort($density, "sortByPopulation");
print_r($density);
?>
</pre>
Output:
Array
(
[Delaware] => Array
(
[Density] => 539
[Population] => 1000000
)
[Connecticut] => Array
(
[Density] => 758
[Population] => 3600000
)
[Maryland] => Array
(
[Density] => 645
[Population] => 6200000
)
[Massachusetts] => Array
(
[Density] => 914
[Population] => 7100000
)
[New Jersey] => Array
(
[Density] => 1291
[Population] => 9500000
)
)
Example 3: Sorting with uasort() function using multiple criteria-
<pre>
<?php
function sortByPopuNDensity( $a, $b){
$activeCompare = $a['Population'] <=> $b['Population'];
if ($activeCompare !== 0) {
return $activeCompare;
}
return $b['Density'] <=> $a['Density'];
}
$density = [
"New Jersey" => ["Density" => 1291, "Population" => 9500000],
"Massachusetts" => ["Density" => 914, "Population" => 7100000],
"Connecticut" => ["Density" => 758, "Population" => 3600000],
"Maryland" => ["Density" => 645, "Population" => 6200000],
"Delaware" => ["Density" => 539, "Population" => 1000000]
];
usort($density, "sortByPopuNDensity");
print_r($density);
?>
</pre>
Output:
Array
(
[3] => Array
(
[State] => Maryland
[Density] => 645
[Population] => 1000000
)
[4] => Array
(
[State] => Delaware
[Density] => 539
[Population] => 1000000
)
[2] => Array
(
[State] => Connecticut
[Density] => 758
[Population] => 3600000
)
[1] => Array
(
[State] => Massachusetts
[Density] => 914
[Population] => 7100000
)
[0] => Array
(
[State] => New Jersey
[Density] => 1291
[Population] => 9500000
)
Notes on uasort() Function:
The function reindexes the keys of the sorted array starting with 0. So, it doesn’t preserve the keys of the original array.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP uasort() Function
The uasort() function is a built-in PHP function and part of the PHP’s array functions. It is a very powerful sorting function that allows you to sort according to your conditions preserving the key-value association.