Connecting Tech Pros Worldwide Help | Site Map

avoiding duplicate array elements

Robert
Guest
 
Posts: n/a
#1: Jul 17 '05
Im a beginner in PHP and Im having a problem with this code. Im
trying to remove duplicate elements from an array created via $_GET.
I want users to be able to click on a link which sends an email
address to an array. I just want to remove duplicate email addresses
from the array. Ive tried array_unique() on my test server but it
doesnt work. So i tried to remove duplicates myself before storing
them into the array. The script works great without the checking, but
I cant have duplicate emails in the array...please help!

session_start();

$email = $HTTP_GET_VARS['email'];
if (isset($_SESSION['Array']))
{

$Array = $_SESSION['Array'];
$numElements = count($Array);
for($counter=0; $counter < $numElements; $counter++)
{
/* Problem is here */ if ($Array[$counter] == $email)
{
exit();
}

else
{
array_push($_SESSION['Array'],$email);
$EmailArray = $_SESSION['Array'];
$_SESSION['Array'] = $EmailArray;
}
}
}



else
{
$EmailArray = array();
array_push($EmailArray,$email);
$_SESSION['Array'] = $EmailArray;
}
Nikolai Chuvakhin
Guest
 
Posts: n/a
#2: Jul 17 '05

re: avoiding duplicate array elements


gojuka@si.rr.com (Robert) wrote in message
news:<742c30b4.0308201839.5ae594c9@posting.google. com>...[color=blue]
>
> Im a beginner in PHP and Im having a problem with this code. Im
> trying to remove duplicate elements from an array created via $_GET.[/color]

I think you are dealing with the problem at the wrong end. Rather
than remove duplicates, try not to allow them. There is a function
called in_array(), which allows you to check if a variable is already
in array. So you could do something like this:

if (!in_array ($address, $addresses)) {
$addresses[] = $address;
}

Cheers,
NC
Robert
Guest
 
Posts: n/a
#3: Jul 17 '05

re: avoiding duplicate array elements


nc@iname.com (Nikolai Chuvakhin) wrote in message news:<32d7a63c.0308202103.1d39f3ae@posting.google. com>...[color=blue]
> gojuka@si.rr.com (Robert) wrote in message
> news:<742c30b4.0308201839.5ae594c9@posting.google. com>...[color=green]
> >
> > Im a beginner in PHP and Im having a problem with this code. Im
> > trying to remove duplicate elements from an array created via $_GET.[/color]
>
> I think you are dealing with the problem at the wrong end. Rather
> than remove duplicates, try not to allow them. There is a function
> called in_array(), which allows you to check if a variable is already
> in array. So you could do something like this:
>
> if (!in_array ($address, $addresses)) {
> $addresses[] = $address;
> }
>
> Cheers,
> NC[/color]


Thanks alot...That solved my problem!
Rob
Kevin Thorpe
Guest
 
Posts: n/a
#4: Jul 17 '05

re: avoiding duplicate array elements


Robert wrote:[color=blue]
> Im a beginner in PHP and Im having a problem with this code. Im
> trying to remove duplicate elements from an array created via $_GET.
> I want users to be able to click on a link which sends an email
> address to an array. I just want to remove duplicate email addresses
> from the array. Ive tried array_unique() on my test server but it
> doesnt work. So i tried to remove duplicates myself before storing
> them into the array. The script works great without the checking, but
> I cant have duplicate emails in the array...please help![/color]

If you are stuck with an array with duplicates...

$unique_array = array_keys(array_flip($array));

Only works for numerically indexed arrays of integers or strings.



Newbie
 
Join Date: Jun 2006
Posts: 1
#5: Jun 30 '06

re: avoiding duplicate array elements


Quote:
$unique_array = array_keys(array_flip($array));
Thank you for the above solution, that is quite an elegant solution. After spending about four hours trying to come up with a solution I am quite humbled by your amazing solution. I tried it and it works perfectly.

Quote:
I think you are dealing with the problem at the wrong end. Rather
than remove duplicates, try not to allow them. There is a function
called in_array(), which allows you to check if a variable is already
in array. So you could do something like this
That is very good advice to check for duplicates I will kept this in mind also but I had arrays I needed to remove duplicates from that were already made.

I just want to thank everyone for their posts that have been so helpful.
Closed Thread