The uksort Function
The uksort function sorts an array
by key values, using a
callback to determine the order
of elements in the sorted array.
The function modifies the array itself.
The comparison function must return an integer, which depending on the comparison result: less than, equal to, or greater than zero.
Syntax
uksort(array &$array, int $flags = SORT_REGULAR): bool
Example
Let's sort the array by keys in ascending order:
<?php
$arr = [
'b' => 1,
'e' => 3,
'c' => 2,
'a' => 5,
'd' => 4,
];
function func($a, $b)
{
if ($a === $b) {
return 0;
} else if ($a < $b) {
return -1;
} else {
return 1;
}
}
uksort($arr, 'func');
var_dump($arr);
?>
Code execution result:
[
'a' => 5,
'b' => 1,
'c' => 2,
'd' => 4,
'e' => 3,
]
See Also
-
the
sortfunction,
which sorts by element values in ascending order -
the
rsortfunction,
which sorts by element values in descending order -
the
ksortfunction,
which sorts by keys in ascending order -
the
krsortfunction,
which sorts by keys in descending order -
the
asortfunction,
which sorts by element values in ascending order while preserving keys -
the
arsortfunction,
which sorts by element values in descending order while preserving keys -
the
natsortfunction,
which sorts using natural order -
the
natcasesortfunction,
which sorts using natural order case-insensitively -
the
usortfunction,
which sorts using a callback -
the
uksortfunction,
which sorts by keys using a callback -
the
uasortfunction,
which sorts using a callback while preserving keys -
the
array_multisortfunction,
which sorts multiple arrays