CKTIS / PHP
uksort()
Sort a map by passing keys to an integer comparator.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
uksort(array &$array, callable $callback): trueSort a map by passing keys to an integer comparator. Only the sign of the callback result matters and zero preserves the original order of equal keys in PHP 8. The values remain attached, allowing domain-specific key ordering.
Signature
uksort(array &$array, callable $callback): true
Example output
{"x":4,"ab":9,"long":7}
Examples
<?php
$tags = ["long"=>7,"x"=>4,"ab"=>9];
uksort($tags, fn($left,$right) => strlen($left) <=> strlen($right));
echo json_encode($tags);
Result
{"x":4,"ab":9,"long":7}