RE: array_unique & multi-dimensional arrays
| From: | Tim Ward | Date: | Tue, 20 Aug 2002 07:58:01 +0000 |
| Subject: | RE: array_unique & multi-dimensional arrays | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-113300@lists.php.net to get a copy of this message | ||
array_unique tests the values of each element of the top level array - which
are all 'array' - hence you only get one element left
the way I'd do this is ...
$archive_nav["$year/$month"] = array(month => $month, year => $year,
longmonth => $longmonth);
this will prevent duplicate combinations of month and year (and,
incidentally, allow you to sort on year and month very easily).
Tim Ward
www.chessish.com
> -----Original Message-----
> From: sasha [mailto:spam@bittersweet2.com]
> Sent: 19 August 2002 19:10
> To: php-general@lists.php.net
> Subject: array_unique & multi-dimensional arrays
>
>
> I am trying to clean up some junky code in a journal/news type
> script and redoing the archive navigation. I am pulling all of
> the dates of the entries and pushing it into a multi-
> dimensional array like so:
>
> array_push($archive_nav, array(month => $month, year => $year,
> longmonth => $longmonth));
>
> I assumed I could just use array_unique to filter out all of
> the duplicates in the array. But it doesn't seem to work that
> way, and the only month/year combo I end up with is the very
> oldest one (according to year/month) in the array.
>
> $archive_nav = array_unique($archive_nav);
>
> If I leave out array_unique, I am getting all of the
> appropriate month/year combos in my script while doing a
> foreach... just one for every single entry in the database for
> that combo (which is a lot in some cases!).
>
> Is there a better way to do this?
>
> sasha
>
>
>