Solved

array_unique,  Can you ignore case sensitivity?

Posted on 2013-06-01
4
911 Views
Last Modified: 2013-06-01
I have a php array that has multiples of the same email address.  I want to use something like array_unique to get a list of unique emaill addresses.  However I am finding that if the same email address where the letter case is used differently array_unique sees them as two different items.  ie.  David@webcahoot.com and david@WebCahoot.com are not unique in $result.  How can I make this happen. Thanks

$result = array_unique($email_list);

Open in new window

0
Comment
Question by:Luey
  • 2
4 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39213397
There is a caveat in your question - email addresses can be case sensitive.
The part before the @, anything after it should be lowercase.
0
 

Author Comment

by:Luey
ID: 39213419
What I am trying to eliminate is sending the same email to

David@Webcahoot.com
david@webcahoot.com
DAVID@WEBCAHOOT.com

any suggestions.  Can't I just make it all lower case with strtolower();  before creating my array?
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 250 total points
ID: 39213423
http://www.php.net/manual/en/function.array-unique.php#78801

function array_iunique($email_list) {
     return array_intersect_key($email_list,array_unique(
                  array_map('strtolower',$email_list)));
 }
0
 
LVL 2

Accepted Solution

by:
vid_yag earned 250 total points
ID: 39213439
Hi Luey,
You can get unique value with following code:
<?php
$emailArr = array(
'David@webcahoot.com',
'david@WebCahoot.com',
'abc@KKK.com',
'ABC@kkk.com'
);
$uniqueArray =  array_intersect_key($emailArr, array_unique(array_map("StrToLower",$emailArr)));
print_r($uniqueArray);
?>

Open in new window

This outputs:
Array ( [0] => David@webcahoot.com [2] => abc@KKK.com ) 

Open in new window



P.S. I gave same answer as GaryC123
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
setup wamp server for first time 2 43
php refresh button on the browser 2 35
Form Processing in PHP 11 31
Access Object's Property 9 22
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now