Using uksort() to Sort an Associative Array by the Length of Its Keys : Array sort : Data Structure PHP Source Code


PHP Source Code » Data Structure » Array sort »

 

Using uksort() to Sort an Associative Array by the Length of Its Keys



<?php
$exes = arrayx => 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";
?>

           
       



HTML code for linking to this page:

Follow Navioo On Twitter

PHP Source Code

 Navioo Data Structure
» Array sort