You want to use usort, which lets you specify a comparison function.

function cmpByLength($a, $b) {
  return strlen($a) > strlen($b);
}

usort($arrayToSort, 'cmpByLength');

And change the '>' to '<' if the ordering is wrong.

Rolf Brusletto wrote:
Hey all -

I've been scouring the php.net site for a few hours now trying to find a viable way to sort a single dimensional array( array('0'=>'fddfsdsfdds', '1'=>','ddddd','2' => 'gofofle'); )..... so that the longest lengthed item is at the top, and the smallest lengthed item is at the bottom.... is there a function to do this... example:

$arrayToSort = array('0' => 'shorter_length', '1' => 'longer_than_longer_than_shorter_length', '2' => 'longer_than_shorter_length');

and I would like to get... whether the keys change or not..

$arrayToSort[0] == 'longer_than_longer_than_shorter_length';
$arrayToSort[1] == 'longer_than_shorter_length';
$arrayToSort[2] == 'shorter_length';


any ideas?


Rolf Brusletto
www.phpExamples.net

-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to