RE: [PHP] removing all duplicate values from an array
| From: | Jeremy Russell | Date: | Thu, 30 Oct 2003 22:04:33 +0000 |
| Subject: | RE: [PHP] removing all duplicate values from an array | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-168188@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Mike Migurski [mailto:mike@saturn5.com]
> Sent: Thursday, October 30, 2003 1:37 PM
> To: Allowee
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] removing all duplicate values from an array
>
> >>I'm looking for a function that does almost the same as
array_unique()
> >>
> >>But it must also delete the other duplicate entry.
> ><snip>
> >
> >Untested pseudo-PHP follows -
> >
> > $encountered_elements = array();
> > foreach($original_array as $key => $val)
> > if(in_array($val, $encountered_elements)) {
> > unset($original_array[$key]);
> > } else {
> > $encountered_elements[$key] = $val;
> > }
>
> Oops, that should have been:
>
> $encountered_elements = array();
> foreach($original_array as $key => $val)
> if(in_array($val, $encountered_elements)) {
> unset($original_array[$encountered_elements[$val]]);
> } else {
> $encountered_elements[$val] = $key;
> }
>
>
I was running Allowee's example array..
array(
'a' => 'one',
'b' => 'one',
'c' => 'zero',
'd' => 'two'
);
through this and am having a hind time with then logic... Could you
explain how the output would be array('c' => 'zero', 'd' =>
'two')??
Thanks