0

I have two pretty big arrays with email addresses in them.

$oldmail and $newmail.

Both looks like this:

[0] => some@email.com
[1] => some1@email.com
[2] => some2@email.com
...

I want to find all the email values in $newmail that does not exist anywhere in $oldmail.

I think this should work:

foreach ($oldmail as $key => $value) 
{
    foreach ($newmail as $key2 => $value2) 
    {
        if ($value == $value2) 
        {
            //do nothing..
        }
        else
        {
            echo $value2;
        }
    }
}

But it is way to resource heavy with big lists.

Is there another more effecient way I can do this?

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
Bolli
  • 4,974
  • 6
  • 31
  • 47
  • I tried array_diff but it does not seam to search through all values - only compares value[0] vs. value2[0] if I understand correct? It shoud compare value[0] against all values in the other array – Bolli Mar 24 '17 at 13:56
  • 1
    should the comparison be case-insensitive? – RomanPerekhrest Mar 24 '17 at 13:56
  • Ahh thats why array_diff() didnt work before, I forgot to strtolower() all the strings :) Thanks! – Bolli Mar 24 '17 at 14:04
  • @Bolli, accept one of the answers or delete your question if your problem is resolved. Thanks, – Abhay Maurya Mar 24 '17 at 14:30

3 Answers3

4

PHP code demo

<?php
$a1=array("red","green","blue","yellow");
$a2=array("red","green","blue");

$result=array_diff($a1,$a2);
print_r($result);
?>
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
1

Use array_diff in PHP

$a1=array("some@email.com","some1@email.com");
$a2=array("some1@email.com","some2@email.com");
$result=array_diff($a2,$a1);
print_r($result);

Result:

Array ( [1] => some2@email.com ) 
Ramalingam Perumal
  • 1,367
  • 2
  • 17
  • 46
1

array_diff() is right choice. It doesn't only matches by index as you mentioned in your comment. It compares all values.

Give this a shot:

$result=array_diff($newmail,$oldmail);
print_r($result);
Abhay Maurya
  • 11,819
  • 8
  • 46
  • 64