0

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

$oldmail and $newmail.

Both looks like this:

[0] => [email protected]
[1] => [email protected]
[2] => [email protected]
...

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?

4
  • 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 Commented Mar 24, 2017 at 13:56
  • 1
    should the comparison be case-insensitive? Commented Mar 24, 2017 at 13:56
  • Ahh thats why array_diff() didnt work before, I forgot to strtolower() all the strings :) Thanks! Commented Mar 24, 2017 at 14:04
  • @Bolli, accept one of the answers or delete your question if your problem is resolved. Thanks, Commented Mar 24, 2017 at 14:30

3 Answers 3

4

PHP code demo

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

$result=array_diff($a1,$a2);
print_r($result);
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Use array_diff in PHP

$a1=array("[email protected]","[email protected]");
$a2=array("[email protected]","[email protected]");
$result=array_diff($a2,$a1);
print_r($result);

Result:

Array ( [1] => [email protected] ) 

2 Comments

Why did you copy the other answer?
thats just the snippet from the official php manual entry for array_diff. They probably both just copied that from there.
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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.