473,756 Members | 8,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

avoiding duplicate array elements

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($_SESSIO N['Array']))
{

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

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

else
{
$EmailArray = array();
array_push($Ema ilArray,$email) ;
$_SESSION['Array'] = $EmailArray;
}
Jul 16 '05 #1
4 13839
go****@si.rr.co m (Robert) wrote in message
news:<74******* *************** ****@posting.go ogle.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
nc@iname.com (Nikolai Chuvakhin) wrote in message news:<32******* *************** ****@posting.go ogle.com>...
go****@si.rr.co m (Robert) wrote in message
news:<74******* *************** ****@posting.go ogle.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
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(arra y_flip($array)) ;

Only works for numerically indexed arrays of integers or strings.

Jul 16 '05 #4
jkjr
1 New Member
$unique_array = array_keys(arra y_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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3223
by: Giloosh | last post by:
Hello, i need some help if possible... i have a payments table with over 500 records i want to run a query that searches through the table spotting out any duplicate ID#'s and Dates. So basically it will run a search spotting out duplicate ID#'s. Than with in those ID#'s spot out duplicate dates. I could do this by eye and spend hours spotting out the duplicated dates, but it would be much easier and funner having a script do it for you.
9
3016
by: NiQ | last post by:
Hello every one, this is about an array of string and may be have done several time before but i need it and wasnt able to find it so here is the problem i have an array of strings with contains words and i want a distinct list of array with the count of each number repeat i.e how many time it exist in the array and the final result should be sorted according to the number of repeat from hight to low . for example
7
23935
by: Voetleuce en fênsievry | last post by:
Hello everyone. I'm not a JavaScript author myself, but I'm looking for a method to remove duplicate words from a piece of text. This text would presumably be pasted into a text box. I have, for example, a list of town names, but there are hundreds of duplicates, like: "Aberdeen Aberdeen Aberdeen Edinburg Edinburg Inverness etc."
11
1394
by: google_groups3 | last post by:
Hi all. I currently have 2 text files which contain lists of file names. These text files are updated by my code. What I want to do is be able to merge these text files discarding the duplicates. And to make it harder (or not???!!) my criteria for defining the duplicate is the left 15 (or so) characters of the file path.
12
1416
by: google_groups3 | last post by:
Hi all. I currently have 2 text files which contain lists of file names. These text files are updated by my code. What I want to do is be able to merge these text files discarding the duplicates. And to make it harder (or not???!!) my criteria for defining the duplicate is the left 15 (or so) characters of the file path. Help, as always, is greatly appreciated!
1
3483
by: gaikokujinkyofusho | last post by:
Hi, I have been enjoying being able to subscribe to RSS (http://kinja.com/user/thedigestibleaggie) for awhile and have come up with a fairly nice list of feeds but I have run into an annoying (though not critical) problem, duplicate stories. Apparently there is overlap with some of the sites I subscribe to so I get duplicate stories. Does anyone know of some sort of filter (software or online service) that can remove duplicate stories? Any...
14
3021
by: mast2as | last post by:
Hi everyone, I am trying to implement some specs which specify that an array of parameter is passed to a function as a pointer to an array terminated by a NULL chatacter. That seemed fairly easy to implement. I had a special Malloc function that would allocated the number of needed bytes for the objects i needed to store + 1 additional byte to save the NULL character /*!
3
5548
by: IZZI | last post by:
This code is created to find all duplicates in a list of number by using a binary search tree implemented as an array. I want to add a function to count how many numbers in the list are duplicated but i dont know where to put it. It's really confusing. One more, i want to increase my array size to 5,000,000 but the program doesnt work.I dont know how to fix it. Plz help me.Thanks 4 advance. #include <stdlib.h> #include <stdio.h> ...
3
5481
by: spl | last post by:
What is the fastest way to find a duplicate value in an array of 10 elements. I have to find just any one first occurrence of the duplication. Lets say I have ary={10, 20, 40, 90, 30, 60, 35, 40, 85, 90}. Here if I have to 40 is duplicated, that's fine, I don't required to find 90 is also repeating. I don't want to use STLs. Can suggest any fast algorithm for this?
0
9973
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9645
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8645
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5247
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.