Re: Sorting an array

From: Date: Wed, 02 Jun 2004 17:07:45 +0000
Subject: Re: Sorting an array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-187464@lists.php.net to get a copy of this message
On Wed, 2004-06-02 at 12:59, John Nichel wrote: > Okay, I know someone is going to shoot me for asking such a dumb > question, but I just can't seem to find the answer anywhere. I have a > multidimensional array which I would like to sort on the value of one of > the keys... > > Array > ( > [0] => Array > ( > [foo] => blah > [bar] => blah > [sort] => 5 > ) > [1] => Array > ( > [foo] => blah > [bar] => blah > [sort] => 2 > ) > [2] => Array > ( > [foo] => blah > [bar] => blah > [sort] => 4 > ) > [3] => Array > ( > [foo] => blah > [bar] => blah > [sort] => 3 > ) > [4] => Array > ( > [foo] => blah > [bar] => blah > [sort] => 1 > ) > ) > > What I would like to do is sort this on the value of 'sort' in each > sub-array. What am I missing???? TIA usort( $theAboveArray, 'mySortHandler' ); function mySortHandler( $v1, $v2 ) { if( $v1['sort'] > $v2['sort'] ) { return 1; } else if( $v1['sort'] < $v2['sort'] ) { return -1; } return 0; } Cheers, Rob. -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

« previous php.general (#187464) next »