In PHP associative arrays it may be useful to sort by the string length (size of the key).
Here's how to do it:
<?php
$a = array ("11" => "11", "111" => "111", "11111" => "11111", "1111" => "1111", "1" => "1");
function cmp($a, $b) {
$lena = strlen($a);
$lenb = strlen($b);
if ($lena == $lenb) return 0;
return ($lena > $lenb) ? -1 : 1;
}
uksort($a, "cmp");
foreach ($a as $key => $value) {
echo "$key : $value<br>";
}
?>
This will produce:
11111 : 11111
1111 : 1111
111 : 111
11 : 11
1 : 1
|