PHP: Array key whitelist and blacklist

Published:

Keep forgetting... writing it down so I can stop googling it...

Whitelist

function whitelist(array $array, array $whitelist)
{
    return array_intersect_key($array, array_flip($whitelist));
}
$filtered = whitelist($array, ['good1', 'good2', ... ]);

Blacklist

function blacklist(array $array, array $blacklist)
{
    return array_diff_key($array, array_flip($blacklist));
}
$filtered = blacklist($array, ['bad1', 'bad2', ...]);