Using uksort() to Sort an Associative Array by the Length of Its Keys
<?php
$exes = array( x => 1,
xxx => 3,
xx => 2,
xxxxx => 4,
xxxxxx => 5
);
function priceCmp( $a, $b ){
if ( strlen( $a ) == strlen( $b ) )
return 0;
if ( strlen( $a ) < strlen( $b ) )
return -1;
return 1;
}
uksort( $exes, priceCmp );
foreach ( $exes as $key => $val )
print "$key: $val<BR>\n";
?>
Related examples in the same category