By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,407 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

avoiding duplicate array elements

P: n/a
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;
}
Jul 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
go****@si.rr.com (Robert) wrote in message
news:<74**************************@posting.google. com>...

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 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
Jul 16 '05 #2

P: n/a
nc@iname.com (Nikolai Chuvakhin) wrote in message news:<32**************************@posting.google. com>...
go****@si.rr.com (Robert) wrote in message
news:<74**************************@posting.google. com>...

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 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

Thanks alot...That solved my problem!
Rob
Jul 16 '05 #3

P: n/a
Robert wrote:
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!


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.

Jul 16 '05 #4

P: 1
$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.

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.
Jun 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.