开发者

Remove elements from array1 that are in array2

开发者 https://www.devze.com 2023-03-12 04:52 出处:网络
I have arrays that looks like this: $array1 = array( \'peter@example.com\' => array( \'peter\' => \'Smith\',

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

精彩评论

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