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
  • 14,792
  • 4
  • 23
  • 42
Bolli
  • 4,914
  • 6
  • 31
  • 47

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
  • 14,792
  • 4
  • 23
  • 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,375
  • 1
  • 17
  • 45
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
  • 10,668
  • 6
  • 42
  • 58