Sorting a Two-Dimensional Array
| From: | Waldo L. Jaquith | Date: | Fri, 14 Jul 2000 22:21:43 +0000 |
| Subject: | Sorting a Two-Dimensional Array | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-6707@lists.php.net to get a copy of this message | ||
Folks,
I've searched the archives over for this, but I can't find a response that's
really pertinent to what I'm doing. I'm new at multi-dimensional arrays, so
please bear with me. :)
I've got an array like this:
$array[1][id]
$array[1][rating]
$array[2][id]
$array[2][rating]
$array[3][id]
$array[3][rating]
I'd like to sort by rating. So if the data looked like this:
$array[1][501]
$array[1][5]
$array[2][467]
$array[2][1]
$array[3][468]
$array[3][3]
I'd like to sort by rating, for it to end up looking like this:
$array[2][467]
$array[2][1]
$array[3][468]
$array[3][3]
$array[1][501]
$array[1][5]
You'll note that it's now in order of 1, 3, 5, instead of 5, 1, 3. In SQL,
this would be wicked easy. I could just do "ORDER BY rating". With
two-dimensional arrays, of course, it's not so easy. I'm aware that I
should be using usort, but I don't know how to go about applying it. I was
hoping for something like "usort($array,rating,asc)", but Momma always
called me a dreamer. :)
Thanks in advance for any help you can offer, fellow PHPers.
Best,
Waldo
Waldo L. Jaquith
< waldo@munkandphyber.com >
< 804/979.2980 >
// Munk & Phyber
// 110B Second Street NE
// Charlottesville, VA 22902