Re: Sorting an array by value length.

From: Date: Wed, 22 Oct 2003 17:24:21 +0000
Subject: Re: Sorting an array by value length.
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-167125@lists.php.net to get a copy of this message
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


« previous php.general (#167125) next »