_tina_ Posted January 28, 2010 Share Posted January 28, 2010 Hi, I have an array of email addresses, I'm trying to figure out how to check for duplicates. I have read various form posts on the topic but can't seem to get anything that works for me. Does anybody have any experience with this? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/190113-email-address-duplicates/ Share on other sites More sharing options...
Andy-H Posted January 28, 2010 Share Posted January 28, 2010 Look into the array_unique function Quote Link to comment https://forums.phpfreaks.com/topic/190113-email-address-duplicates/#findComment-1003099 Share on other sites More sharing options...
ChemicalBliss Posted January 28, 2010 Share Posted January 28, 2010 Yup, my method is too loop through the array and check it manually one by one: Theory = Start Array with Duplicates Create new array (for this purpose ill name it "NewArray"). Loop through each item of Start Array, and add it to NewArray as long as it doesnt exist. Then your left with a new array. An Example: (UNTESTED) <?php $Array = array( "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]" ); $nArray = array(); for($i=0;$i<count($Array);$i++){ if(!isset($nArray[$Array[$i]])){ $nArray[$Array[$i]] = count($nArray) - 1; } } $nArray = array_flip($nArray); ?> Hope this helps, -CB- Quote Link to comment https://forums.phpfreaks.com/topic/190113-email-address-duplicates/#findComment-1003101 Share on other sites More sharing options...
Andy-H Posted January 28, 2010 Share Posted January 28, 2010 <?php $array = array( "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]" ); $array = array_map("strtolower", $array); $unique = array_unique($array, SORT_STRING); echo '<pre>' . "\n\tStart array:\n\n" . print_r($array, true) . "\n\n" . '</pre>'; echo '<pre>' . "\n\tUnique array:\n\n" . print_r($unique, true) . '</pre>'; ?> Output: Start array: Array ( [0] => [email protected] [1] => [email protected] [2] => [email protected] [3] => [email protected] [4] => [email protected] ) Unique array: Array ( [0] => [email protected] [1] => [email protected] [2] => [email protected] [4] => [email protected] ) Quote Link to comment https://forums.phpfreaks.com/topic/190113-email-address-duplicates/#findComment-1003112 Share on other sites More sharing options...
ChemicalBliss Posted January 28, 2010 Share Posted January 28, 2010 Aw lol who will he go for? Quote Link to comment https://forums.phpfreaks.com/topic/190113-email-address-duplicates/#findComment-1003114 Share on other sites More sharing options...
PFMaBiSmAd Posted January 28, 2010 Share Posted January 28, 2010 To actually check for and find duplicates you would want to use array_count_values Quote Link to comment https://forums.phpfreaks.com/topic/190113-email-address-duplicates/#findComment-1003119 Share on other sites More sharing options...
_tina_ Posted January 28, 2010 Author Share Posted January 28, 2010 Thanks for all the replies guys. This is a huge help. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/190113-email-address-duplicates/#findComment-1003167 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.