RE: RE: array_unique & multi-dimensional arrays
| From: | Tim Ward | Date: | Tue, 20 Aug 2002 13:58:02 +0000 |
| Subject: | RE: RE: array_unique & multi-dimensional arrays | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-113322@lists.php.net to get a copy of this message | ||
Sasha ... I'm sending this again to the list as it seemed to bounce from
your address
the way I was doing it prevents unique values from being created in the
first place. The only array that gets overwritten is one that would be the
same any way
while ($row = mysql_fetch_array($result))
{ $month = date("m", $row['timestamp']);
$year = date("Y", $row['timestamp']);
$archive_date = mktime(1,1,1,$month,1,$year);
$archive_nav[$archive_date] = $archive_date;
}
... ensures that if the date has already been added to the array then it
overwrites the existing one instead of adding a new element.
Tim Ward
> -----Original Message-----
> From: sasha [mailto:spam@bittersweet2.com]
> Sent: 20 August 2002 14:36
> To: Tim Ward
> Subject: Re: RE: array_unique & multi-dimensional arrays
>
>
> It may just have been that I was not putting your
> sample code in the right location... I had ended up
> doing it differently than I originally intended:
>
> while ($row = mysql_fetch_array($result)) {
> $month = date("m", $row['timestamp']);
> $year = date("Y", $row['timestamp']);
> $archive_date = mktime(1,1,1,$month,1,$year);
>
> array_push($archive_nav, $archive_date);
> }
> $archive_nav = array_values(array_unique
> ($archive_nav));
>
> And then later on, I do a date check on the new
> timestamp to get the month/year/longmonth out of it.
> Only 3 extra lines of code this way.
>
> I don't understand exactly how your code works. If I
> do it inside of the while loop, it would overwrite the
> previous contents of the array (unless I did a '.=',
> instead of just '='). So when I tried doing it
> outside of the loop, all I ended up with was nothing.
>
> thanks
> sasha
>
> 8/20/2002 3:58:01 AM, Tim Ward
> <Tim.ward@stivesdirect.com> wrote:
>
> >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
> >>
> >>
> >>
> >
>
>
>