ionicle Posted November 22, 2013 Share Posted November 22, 2013 Hey again, everybody. Need help with the solution of yet another task related to php. I have two lists of email addresses - List 1 and List 2, so to say. The entire contents of List 2 is a part of List 1. I would like for PHP to compare both lists and erase all the email addresses out of List 2, that are contained in List 1, plus all the email addresses, located at every domain name, listed within List 2. For instance: List 1: brym@yahoo.com gargamel@yahoo.com grungel@gmail.com shushlek@hotmail.com gushterlqk@aol.com shuslica@web.de List 2: brym@yahoo.com grungel@gmail.com After processing's completed, List 1 should look like this: List 1: shushlek@hotmail.com gushterlqk@aol.com shuslica@web.de How would I go about doing that? Quote Link to comment https://forums.phpfreaks.com/topic/284163-comparing-two-lists-of-email-addresses-with-php/ Share on other sites More sharing options...
cyberRobot Posted November 22, 2013 Share Posted November 22, 2013 Have you looked into array_diff(): http://us2.php.net/manual/en/function.array-diff.php Quote Link to comment https://forums.phpfreaks.com/topic/284163-comparing-two-lists-of-email-addresses-with-php/#findComment-1459518 Share on other sites More sharing options...
ionicle Posted November 22, 2013 Author Share Posted November 22, 2013 (edited) That looks pretty convenient! Thing is, I not only need the entire List 2 to be removed from List 1, but also have all email addresses from List 1, matching with all domain names from List 2, be removed from List 1. array_diff() wouldn't help with that, I guess. Edited November 22, 2013 by ionicle Quote Link to comment https://forums.phpfreaks.com/topic/284163-comparing-two-lists-of-email-addresses-with-php/#findComment-1459524 Share on other sites More sharing options...
cyberRobot Posted November 22, 2013 Share Posted November 22, 2013 Does array_diff() return the desired results? If so, you could assign the results back to the variable holding list 1. Quote Link to comment https://forums.phpfreaks.com/topic/284163-comparing-two-lists-of-email-addresses-with-php/#findComment-1459530 Share on other sites More sharing options...
JIXO Posted November 22, 2013 Share Posted November 22, 2013 (edited) Try this <?php // LIST 1 $list1 = array( "brym@yahoo.com", "gargamel@yahoo.com", "grungel@gmail.com", "shushlek@hotmail.com", "gushterlqk@aol.com", "shuslica@web.de" ); // LIST 2 $list2 = array( "brym@yahoo.com", "grungel@gmail.com" ); function extractUnwantedDomains($list2) { $list2Domains = array(); foreach($list2 as $email) { $atPosition = strripos($email, '@'); $dotPosition = strripos($email, '.', $atPosition + 1); $list2Domains[] = substr($email, ($atPosition + 1), ($dotPosition - 1) - ($atPosition)); } return($list2Domains); } function extractFinalList($list1, $list2) { $finalList = array(); $difference = array_diff($list1, $list2); $unwantedDomains = extractUnwantedDomains($list2); foreach($difference as $email) { foreach($unwantedDomains as $domain) { if(preg_match("/{$domain}/", $email)) { continue(2); } } $finalList[] = $email; } return($finalList); } $finalList = extractFinalList($list1, $list2); var_dump($finalList); Edited November 22, 2013 by JIXO Quote Link to comment https://forums.phpfreaks.com/topic/284163-comparing-two-lists-of-email-addresses-with-php/#findComment-1459532 Share on other sites More sharing options...
cyberRobot Posted November 22, 2013 Share Posted November 22, 2013 (edited) Nevermind....I just re-read the question and noticed my solution doesn't quite meet your needs. Edited November 22, 2013 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/284163-comparing-two-lists-of-email-addresses-with-php/#findComment-1459534 Share on other sites More sharing options...
cyberRobot Posted November 22, 2013 Share Posted November 22, 2013 You could try using array_diff() to get the difference. And then adapt the solution provided a few days ago to get unique domains: http://forums.phpfreaks.com/topic/284098-pick-a-random-email-address-out-of-a-maillist/?do=findComment&comment=1459187 Quote Link to comment https://forums.phpfreaks.com/topic/284163-comparing-two-lists-of-email-addresses-with-php/#findComment-1459535 Share on other sites More sharing options...
mac_gyver Posted November 22, 2013 Share Posted November 22, 2013 (edited) just satisfying your second condition of removing all entries with the same domains found in the second list will remove the specific emails in the first condition. the quickest code would be to preprocess the arrays of addresses so that you have an array of arrays, where the main key is the domain name (shown in pseudo code form, not the actual array) - $array1['yahoo.com'] = array(brym@yahoo.com,gargamel@yahoo.com) $array1['gmail.com'] = array(grungel@gmail.com) $array1['hotmail.com'] = array(shushlek@hotmail.com) ... $array2 .... then (untested), you should be able to use array_diff() and the main key values in the second list will remove all the corresponding domain entries in the first list. Edited November 22, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/284163-comparing-two-lists-of-email-addresses-with-php/#findComment-1459538 Share on other sites More sharing options...
mac_gyver Posted November 22, 2013 Share Posted November 22, 2013 using array_diff_key() - $list1 = array( "brym@yahoo.com", "gargamel@yahoo.com", "grungel@gmail.com", "shushlek@hotmail.com", "gushterlqk@aol.com", "shuslica@web.de" ); $list2 = array( "brym@yahoo.com", "grungel@gmail.com" ); $array1 = array(); foreach($list1 as $email){ list($name,$domain) = explode('@',$email); if(!isset($array1[$domain])){$array1[$domain] = array();} $array1[$domain][] = $email; } $array2 = array(); foreach($list2 as $email){ list($name,$domain) = explode('@',$email); if(!isset($array2[$domain])){$array2[$domain] = array();} $array2[$domain][] = $email; } $result = array_diff_key($array1,$array2); $final = array(); foreach($result as $arr){ // there should be a way to do this without a loop... $final = array_merge($final,$arr); } echo '<pre>'; print_r($final); Quote Link to comment https://forums.phpfreaks.com/topic/284163-comparing-two-lists-of-email-addresses-with-php/#findComment-1459549 Share on other sites More sharing options...
Solution ionicle Posted November 22, 2013 Author Solution Share Posted November 22, 2013 Turns out JIXO's code works. For some reason though, when I load up a large number of email addies in the arrays, it screws up and spits out a blank result. No clue why. I got another reply on Stackoverflow: <?php function domain($email){ $x=explode('@',$email); return $x[1]; } $list1=array("brym@yahoo.com","gargamel@yahoo.com","grungel@gmail.com","shushlek@hotmail.com","gushterlqk@aol.com","shuslica@web.de");//first list $list2=array("brym@yahoo.com","grungel@gmail.com");//second list $black_domains=array(); foreach($list2 as $l2){ $black_domains[]=domain($l2); } $new_list1=array(); foreach($list1 as $l1){ $domain=domain($l1); if(!in_array($domain,$black_domains)){ $new_list1[]=$l1; }; } print_r($new_list1); //this gives new list ?> That one works like a charm, even with very large lists. Quote Link to comment https://forums.phpfreaks.com/topic/284163-comparing-two-lists-of-email-addresses-with-php/#findComment-1459578 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.