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

uksort

Description

Sort an array by keys using a user-defined comparison function

Syntax

uksort ( array &$array , callable $key_compare_func ) : bool

Parameters

array

The input array.

key_compare_func

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.

Return

Returns TRUE on success or FALSE on failure.

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);
$key_compare_func = "myfunction";

uksort($array, $key_compare_func);

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

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