开发者

Remove elements from array1 that are in array2

I have arrays that looks like this:

$array1 = array(
    'peter@example.com' => array(
        'peter' => 'Smith',
    ),
    'john@example.com' => array(
        'john' => 'Smith',
    ),
    'louis@example.com' => array(
        'louis' => 'Smith',
    ),
    'jane@example.com' => array(
        'jane' => 'Smith',
    ),
);


$array2 = array(
    '0' => 'peter@example.com',
    '1' => 'john@example.com',
);

How do I remove the array e开发者_如何学Pythonlements in array1 that match array2?


As simple as:

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


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]);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜