What is PHP usort() Function?
If you want to sort an array items based on custom rules or/and based on multiple fields, use usort() function. You can perform complex sorting or multidimensional array sorting with this function.
How PHP usort() Function Works (Sorts)?
The usort() function has a callback function (which is a user-defined comparison function) in its second parameter. It is the heart of the usort() function as it determines the order of the elements of the array. The array is the first parameter of the usort() function. The comparison function 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-
- 0: If both elements are equal.
- Greater than 0 (ex. 1): If 1st element is larger than the 2nd element.
- Less than 0 (ex.-1): If 1st element is smaller than the 2nd element.
Syntax:
usort(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:
<pre>
<?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
(
[0] => 539
[1] => 645
[2] => 758
[3] => 914
[4] => 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
(
[0] => Array
(
[Density] => 539
[Population] => 1000000
)
[1] => Array
(
[Density] => 758
[Population] => 3600000
)
[2] => Array
(
[Density] => 645
[Population] => 6200000
)
[3] => Array
(
[Density] => 914
[Population] => 7100000
)
[4] => Array
(
[Density] => 1291
[Population] => 9500000
)
)
Example 3: Sorting with usort() 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
(
[0] => Array
(
[Density] => 539
[Population] => 1000000
)
[1] => Array
(
[Density] => 758
[Population] => 3600000
)
[2] => Array
(
[Density] => 645
[Population] => 6200000
)
[3] => Array
(
[Density] => 914
[Population] => 7100000
)
[4] => Array
(
[Density] => 1291
[Population] => 9500000
)
)
Notes on usort() 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 usort() Function
The usort() 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.