php-general | 2000071
Date: 07/14/00
- Next message: Donald Allen: "[PHP] Layout Wizards"
- Previous message: David Banning: "[PHP] newbie problem - code shows no output"
- In reply to: Waldo L. Jaquith: "[PHP] Sorting a Two-Dimensional Array"
- Next in thread: Christopher Thompson: "RE: [PHP] Sorting a Two-Dimensional Array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
""Waldo L. Jaquith"" <waldo <email protected>> wrote in message
news:B5950AB7.A046%waldo <email protected>
> Folks,
<snip>
> 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]
> 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. :)
In SQL, you would probably store ID and RATING in two separate tables and
JOIN them, or store them as fields in the same table. What you have here is
not really an analogous setup.
Why isn't the ID the index? Sorting on RATING would then be easier. If the
IDs aren't sequential, use an associative array. Sorting would then be done
using asort().
Also, to better mimic the SQL you know and love, you might use a construct
like this instead of multidimensional arrays ...
$array[index] = "string,string,string,string";
You can explode $array[index] into an array when you need it. Possibly CPU
and time expensive, but probably easier to bend your mind around than
multi-dimensional arrays. You can use a string as "index", and sort using
the index (with ksort() )or the beginning of each element (with asort() ).
-- O.-- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe <email protected> For additional commands, e-mail: php-general-help <email protected> To contact the list administrators, e-mail: php-list-admin <email protected>
- Next message: Donald Allen: "[PHP] Layout Wizards"
- Previous message: David Banning: "[PHP] newbie problem - code shows no output"
- In reply to: Waldo L. Jaquith: "[PHP] Sorting a Two-Dimensional Array"
- Next in thread: Christopher Thompson: "RE: [PHP] Sorting a Two-Dimensional Array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

