PHP uasort() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, uasort() sorts an array by its values using your own comparison function, and keeps the keys.

The "u" stands for user-defined and the "a" stands for association (keys kept). Use it when the normal sorting rules do not fit, such as sorting products by price or names by length, while keeping each key linked to its value.

Syntax

Call uasort() with the values you need. Start with this line.

PHP
uasort($ages, fn($a, $b) => $a <=> $b);
CallWhat it does
uasort($ages, fn($a, $b) => $a <=> $b)Sorts the values from low to high and keeps the keys.
uasort($ages, fn($a, $b) => $b <=> $a)Swapping $a and $b sorts from high to low.
uasort($items, "strcmp")You can also pass the name of an existing function.

The callback receives two values and must return a negative number, zero or a positive number. The spaceship operator <=> does this for you. uasort() changes the original array and returns true.

Library declarations
PHP
uasort(array &$array, callable $callback): true

Parameters

ParameterDescription
arrayRequired. The array to sort. It is passed by reference and changed in place.
callbackRequired. A function that takes two values and returns an integer less than, equal to or greater than zero. This sets the order.

Return Value

Return ValueDescription
trueAlways returns true. The sorted result is stored in the array you passed in.

PHP Examples

Example 1 — Sort Values Low to High and Keep Keys

The spaceship operator <=> compares two values and gives -1, 0 or 1.

PHP
<?php
$ages = ["Tom" => 30, "Ann" => 25, "Raj" => 35];

uasort($ages, fn($a, $b) => $a <=> $b);

foreach ($ages as $name => $age) {
    echo $name, " => ", $age, PHP_EOL;
}

How It Works

  1. $ages has names as keys and ages as values.
  2. PHP calls the arrow function with two values at a time to decide which one goes first.
  3. <=> returns a negative number when $a is smaller, so smaller ages come first.
  4. The names stay attached to their ages in the loop output.

Example 2 — Sort from High to Low

Switching the two sides of <=> reverses the order.

PHP
<?php
$ages = ["Tom" => 30, "Ann" => 25, "Raj" => 35];

uasort($ages, fn($a, $b) => $b <=> $a);

foreach ($ages as $name => $age) {
    echo $name, " => ", $age, PHP_EOL;
}

How It Works

  1. The same array is used again.
  2. The callback now writes $b <=> $a, so larger values count as "smaller" and move first.
  3. uasort() sorts the pairs from the highest age to the lowest.
  4. The keys are still attached to their values.

Example 3 — Sort Records by One Field

When each value is an array, the callback can compare one field inside it.

PHP
<?php
$products = [
    "p1" => ["name" => "Pen", "price" => 5],
    "p2" => ["name" => "Book", "price" => 12],
    "p3" => ["name" => "Bag", "price" => 3],
];

uasort($products, fn($a, $b) => $a["price"] <=> $b["price"]);

foreach ($products as $id => $product) {
    echo $id, ": ", $product["name"], PHP_EOL;
}

How It Works

  1. Each product is itself an array with a name and a price.
  2. The callback compares the "price" field of the two products it receives.
  3. The cheapest product, Bag, moves to the front, then Pen, then Book.
  4. The keys p1, p2 and p3 stay with their products.

Frequently Asked Questions

It sorts an array by value using a function you write, and keeps the key and value pairs together.
uasort() keeps the keys. usort() replaces them with 0, 1, 2.
uasort() compares the values. uksort() compares the keys.
A negative number if the first value goes first, zero if they are equal, and a positive number if the second goes first. Use the <=> operator.
No. It changes the array you pass in and returns true.

Next: uksort()

uksort() sorts an array by its keys using your own comparison function, and keeps each key.

uksort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful