Re: Sorting an array by value length.

From: Date: Wed, 22 Oct 2003 18:09:17 +0000
Subject: Re: Sorting an array by value length.
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-167133@lists.php.net to get a copy of this message
On Wed, 22 Oct 2003 11:21:13 -0700, you wrote: >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: <?php function cmp ($a, $b) { $a = strlen ($a); $b = strlen ($b); if ($a == $b) return (0); return (($a > $b) ? -1 : 1); } $a = array ('a', 'aa', 'aaa', 'aaaa', 'aaaaa'); usort ($a, "cmp"); print_r ($a); ?> Standard warning: usort() has side-effects:

« previous php.general (#167133) next »