473,597 Members | 2,713 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 13831
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
3216
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
2992
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
23915
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
1385
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
1406
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
3475
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
3011
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
5530
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
5477
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
8276
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...
1
8044
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8261
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
6705
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...
0
5437
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3893
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
3936
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2409
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
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.