Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

uksort

Description

The uksort of Array for PHP sort an array by keys using a user-defined comparison function.

Syntax

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

Parameters

array

The input array.

callback

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

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

first argument

b

second argument

Caution: Returning non-integer values from the comparison function, such as float, will result in an internal cast to integer of the callback's return value. So values such as 0.99 and 0.1 will both be cast to an integer value of 0, which will compare such values as equal.

Return

Always returns true.

Examples

1

<?

function myfunction($a, $b)
{
    $a = preg_replace('@^(a|an|the) @', '', $a);
    $b = preg_replace('@^(a|an|the) @', '', $b);

    return strcasecmp($a, $b);
}

$array = array("John" => 1, "the Earth" => 2, "an apple" => 3, "a banana" => 4);
$callback = "myfunction";

uksort($array, $callback);

foreach($array as $key => $value)
{
    echo "$key: $value\n";
}

?>
an apple: 3
a banana: 4
the Earth: 2
John: 1
HomeMenu