checking for multiple values within an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

checking for multiple values within an array

Post by hame22 »

Hi I have an array of email addresses.

What I would like to do is search this array to see if there are multiples of the same email address, so I can then return an error.

Is there a function that allows you to do this?

thanks in advance.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

if (count($array) !== count(array_unique($array))) echo 'Duplicates detected!';
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Or, as you are loading the addresses into the array:

Code: Select all

if(in_array($thisEmail, $emailAddresses)) { echo "duplicate found"; exit; }
will potentially find duplicates before the entire array is loaded.
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

thanks thats great!
Post Reply