usort: sorting by user defined function

usort(): custom value sorting

usort() sorts values with a callback and reindexes numeric keys. In current PHP, the spaceship operator makes many comparison callbacks concise.

$products = [
    ['name' => 'Book', 'price' => 250],
    ['name' => 'Pen', 'price' => 40],
    ['name' => 'Bag', 'price' => 900],
];
usort($products, fn($a, $b) => $a['price'] <=> $b['price']);
print_r($products);

If original keys matter, choose a key-preserving sorting approach instead of usort().

We can sort an array by using some non-trivial criteria in PHP by using a user defined comparison function. In the function usort we can use our own sort criteria by using one function.

You can read many standard way of sorting the array given here.

Example : Sorting in the order of length

Here is the user defined function has to have some matching requirements. One is it has to accept two variables. It should return either 0,less than 0 or greater than 0 based on the matching where first element is less than , equal to or greater than second element.

After the sorting the small word ( by length ) will be at the first and the longest word will to the end.

This usort function does not retain the associated index or the key of the elements of the array.
<?Php
function compare($x,$y){
$x=strlen($x);
$y=strlen($y);
if($x==$y){
return 0;
}
 return ($x < $y) ? -1 : 1;
}

$input = array ("Pineapple", "Orange", "Banana", "Mango", "Apple","Strawberry","aaa","bb");
foreach ($input as $key => $val) {
echo "$key -> $val <br>";
}
usort($input, "compare");
echo "<br>-----After usort---------<br>";
foreach ($input as $key => $val) {
echo "$key -> $val <br>";
}
?>
Here is the output of usort function where user defined comparisons function is used

0 -> Pineapple 
1 -> Orange
2 -> Banana
3 -> Mango
4 -> Apple
5 -> Strawberry
6 -> aaa
7 -> bb

-----After usort---------
0 -> bb
1 -> aaa
2 -> Apple
3 -> Mango
4 -> Orange
5 -> Banana
6 -> Pineapple
7 -> Strawberry

Example: Sorting a Multidimensional Array by a Key

$array = [
    ['name' => 'John', 'age' => 25],
    ['name' => 'Anna', 'age' => 22],
    ['name' => 'Mike', 'age' => 30]
];
usort($array, function($a, $b) {
    return $a['age'] - $b['age'];
});
print_r($array);
output
Array ( [0] => Array ( [name] => Anna [age] => 22 ) [1] => Array ( [name] => John [age] => 25 ) [2] => Array ( [name] => Mike [age] => 30 ) )

Example: Case-Insensitive String Sorting

$array = ['Banana', 'apple', 'Cherry'];
usort($array, 'strcasecmp');
print_r($array);
Array ( [0] => apple [1] => Banana [2] => Cherry )

Check how file modification time of all files inside a directory is sorted by using usort()
Array REFERENCE array_multisort() :Sorting Multiple Arrays


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer