Cudos
Cudos

Reputation: 5884

Remove elements from array1 that are in array2

I have arrays that looks like this:

$array1 = array(
    '[email protected]' => array(
        'peter' => 'Smith',
    ),
    '[email protected]' => array(
        'john' => 'Smith',
    ),
    '[email protected]' => array(
        'louis' => 'Smith',
    ),
    '[email protected]' => array(
        'jane' => 'Smith',
    ),
);


$array2 = array(
    '0' => '[email protected]',
    '1' => '[email protected]',
);

How do I remove the array elements in array1 that match array2?

Upvotes: 0

Views: 414

Answers (2)

user743234
user743234

Reputation:

Quick and easy (but not as quick and easy as deceze's method, lol)

foreach ($array1 as $key => $value) {
    for ($i = 0; $i < count($array2); $i++) {
        if ($key == $array2[$i]) {
            unset($array1[$key]);
        }
    }
}

Upvotes: 0

deceze
deceze

Reputation: 522015

As simple as:

$diff = array_diff_key($array1, array_flip($array2));

Upvotes: 9

Related Questions