Remove blacklist keys from array in PHP
Solution 1:
$out =
array_diff_key
($data,
array_flip
($bad_keys));
All I did was look through the list of Array functions until I found the one I needed (_diff_key
).
Solution 2:
The solution is indeed the one provided by Niet the Dark Absol. I would like to provide another similar solution for anyone who is after similar thing, but this one uses a whitelist instead of a blacklist:
$whitelist = array( 'good_key1', 'good_key2', ... );
$output = array_intersect_key( $data, array_flip( $whitelist ) );
Which will preserve keys from $whitelist
array and remove the rest.