Re: multidimensional array sorting
| From: | Hugh Danaher | Date: | Sat, 19 Apr 2003 18:56:57 +0000 |
| Subject: | Re: multidimensional array sorting | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-144493@lists.php.net to get a copy of this message | ||
Kelly,
you can use array_multisort() to do what you want. It's a bit convoluted to
work with as you'll need to create a secondary array to hold the sorted
values to compare back to your multidimensional array. The manual is prety
good reference for this
http://www.php.net/manual/en/function.array-multisort.php
though you'll need
to search the user added notes at the bottom for good techniques. I got it
to work some time back but can't remember where that script fragment is
today, otherwise I'd post it.
Hugh
----- Original Message -----
From: "Kelly Protsko" <linuxbox@wepsoft.com>
To: <php-general@lists.php.net>
Sent: Saturday, April 19, 2003 9:27 AM
Subject: [PHP] multidimensional array sorting
> I have a text file which I read into an 2-D array so each row holds the
> date in its pieces and the event title. I want to sort this array based
> on the day of the month in ascending order.
>
> The array is setup like this:
>
> January,7,2003,event title
> January,1,2003,event title
> January,4,2003,event title
>
>
> $list[0][0] = "January";
> $list[0][1] = 3;
> $list[0][2] = 2003;
> $list[0][3] = "event title";
>
>
> This is the way I was trying to sort the array but the order doesn't
> seem to change.
>
> usort($lines,compare);
>
> function compare($x,$y)
> {
>
> If ($x[1] == $y[1])
> return 0;
> else If ($x[1] < $y[1])
> return -1;
> else
> return 1;
> }
>
>
>
> Kelly
>
>