Content · Programming · PHP

PHP usort() Function: Usage and Examples

Learn usort() with clear PHP examples, output, practical scenarios, important behaviors, and common beginner mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP developers sometimes need to sort values with a custom comparison callback and reindex keys. This comes up when preparing data, displaying results, or integrating information with other services.

The usort() operation handles this particular task. The following examples show its result, its limitations, and a practical way to apply it.

What Is usort() in PHP?

Sort values with a custom comparison callback and reindex keys.

Its syntax is:

usort(array &$array, callable $callback): true

The syntax shows the expected parameters and return type. Arguments marked with an ampersand are passed by reference and may be modified. Optional parameters control additional behavior; values must match the declared types.

Important: Numeric keys are rebuilt. Keep record identifiers inside values instead of relying on array keys.

Parameters:

  • &$array — May be updated by the function.
  • $callback — A named input or configuration argument; use the declared type and optional default shown above.

Example 1: Basic Usage

This first example uses a small input to demonstrate the main result of usort().

<?php

$numbers = [8, 2, 5]; usort($numbers, fn($left, $right) => $left <=> $right); print_r($numbers);

Output:

Array
(
    [0] => 2
    [1] => 5
    [2] => 8
)

The output demonstrates the primary operation: sort values with a custom comparison callback and reindex keys.

Example 2: Understanding Another Case

This second example changes the input or option to reveal a different behavior of usort().

<?php

$products = [["price" => 20], ["price" => 10]]; usort($products, fn($left, $right) => $left["price"] <=> $right["price"]); print_r($products);

Output:

Array
(
    [0] => Array
        (
            [price] => 10
        )

    [1] => Array
        (
            [price] => 20
        )

)

Compared with the first example, this one illustrates the same operation with a different input shape or option. Numeric keys are rebuilt. Keep record identifiers inside values instead of relying on array keys.

Practical Example: A Real Application Scenario

Here is a practical use of usort() with realistic sample information. Adapt the values to match your application.

<?php

$tickets = [["id" => 7, "priority" => 2], ["id" => 8, "priority" => 9]]; usort($tickets, fn($left, $right) => $right["priority"] <=> $left["priority"]); foreach ($tickets as $ticket) { echo $ticket["id"] . "\n"; }

Output:

8
7

This is a useful starting point, but production code should check errors and validate values before performing the operation. Numeric keys are rebuilt. Keep record identifiers inside values instead of relying on array keys.

Important Behavior and Edge Cases

Numeric keys are rebuilt. Keep record identifiers inside values instead of relying on array keys.

  • Input: Use the expected PHP types and validate user-provided values.
  • Result: Check the return value separately from any modifications to the input.
  • Environment: For extension, database, or filesystem examples, verify availability and handle failures before accessing results.

Comparison With Related Functions

Function or ApproachWhen to Use It
usort()Sort values with a custom comparison callback and reindex keys.
Related functionsuasort() preserves key associations; sort() uses built-in comparisons.

Common Beginner Mistakes

MistakeCorrect Approach
Expecting a sorted copy without changing the inputThese sort functions modify the array directly; copy it first if you need the original.
Using the wrong sort targetDecide whether to sort keys or values, and whether key associations must remain intact.
Assuming every comparison handles text in the same wayuasort() preserves key associations; sort() uses built-in comparisons.

Key Takeaways

  • usort() lets you sort values with a custom comparison callback and reindex keys.
  • Numeric keys are rebuilt. Keep record identifiers inside values instead of relying on array keys.
  • uasort() preserves key associations; sort() uses built-in comparisons.
  • Check input, return values, and environmental requirements when adapting the examples.

usort() provides a focused PHP operation for sort values with a custom comparison callback and reindex keys. Start with the simple examples, then adapt the practical example with the correct checks for your project.

Share this resourceSend this page to someone who may find it useful.