PHP uasort array function

Last Updated: February 21, 2022

uasort – Sort an array using user-defined functions and maintain index association

Syntax

uasort(array &$array, callable $callback): bool

Supports (PHP 4 , PHP 5, PHP 7, PHP 8)

Parameters

Parameter Description
array input array
callback

Callback function used for sorting

callback(mixed $a, mixed $b): int

Return Value

Always return true

Example

function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

$array1 = array('a', 'c', 'b', 'D', 'E', 'F');

uasort($array1,"cmp");
print_r($array1);

Output

Array ( [3] => D [4] => E [5] => F [0] => a [2] => b [1] => c )

Test