usort() / uasort() / uksort() Since: PHP 4(2000)
Sorts an array using a user-defined comparison function. Use this when the standard sort functions cannot handle the sorting logic you need.
Syntax
// Sorts by value using a user-defined comparison function. Keys are re-indexed. usort(array, callback); // Sorts by value using a user-defined comparison function. Key-value associations are preserved. uasort(array, callback); // Sorts by key using a user-defined comparison function. uksort(array, callback);
Functions
| Function | Description |
|---|---|
| usort($array, $callback) | Sorts an array by value using a comparison function. Numeric keys are re-indexed starting from 0. |
| uasort($array, $callback) | Sorts an array by value using a comparison function. Key-value associations are preserved. |
| uksort($array, $callback) | Sorts an array by key using a comparison function. |
Sample Code
<?php
// Sort product data by price in ascending order.
$products = [
['name' => 'Laptop', 'price' => 98000],
['name' => 'Mouse', 'price' => 2500],
['name' => 'Keyboard', 'price' => 8000],
['name' => 'Monitor', 'price' => 35000],
];
usort($products, function($a, $b) {
return $a['price'] - $b['price']; // Compare prices in ascending order.
});
print_r($products); // Products are listed from cheapest to most expensive.
// You can also use arrow functions introduced in PHP 7.4.
usort($products, fn($a, $b) => $b['price'] - $a['price']); // Descending order by price.
print_r($products); // Products are listed from most expensive to cheapest.
// Comparison using the spaceship operator.
$names = ['Smith', 'Johnson', 'Brown', 'Williams'];
usort($names, fn($a, $b) => $a <=> $b);
print_r($names); // Names are sorted in character code order.
// Sort an associative array by value while preserving key-value associations.
$scores = ['Taro' => 85, 'Hanako' => 92, 'Jiro' => 78];
uasort($scores, fn($a, $b) => $b - $a); // Compare in descending order.
print_r($scores); // Scores are listed highest first, with names preserved.
// Sort keys using a custom criterion.
$data = ['item_3' => 'C', 'item_1' => 'A', 'item_10' => 'J', 'item_2' => 'B'];
uksort($data, fn($a, $b) => strnatcmp($a, $b)); // Compare keys in natural order.
print_r($data); // Keys are sorted in natural numeric order.
Notes
The comparison function takes two arguments and must return a negative number if the first argument is less than the second, zero if they are equal, and a positive number if the first is greater. In PHP 7.0 and later, the spaceship operator '<=>' provides a concise way to return all three of these values.
Arrow functions introduced in PHP 7.4 — written as 'fn($a, $b) =>' — let you write shorter code than anonymous functions. For complex comparison logic, use a regular anonymous function instead.
Because 'usort()' re-indexes keys, use 'uasort()' if you need to preserve the keys of an associative array. For simple ascending or descending sorts, 'sort() / asort()' is more concise.
If you find any errors or copyright issues, please contact us.