PHP’s “associative array”: removing blacklisted or keeping whitelisted keys

Yup, if there is one language I loathe more than PHP, it’s Javascript… but this here is about PHP and how to either remove unwanted keys from an “associative array” (as they call it in PHP world) or leave only those, which you want to stay – in one shot.

Remove unwanted keys:

$my_cleaned_array = array_diff_key($my_dirty_array, array_flip($array_of_blacklisted_keys));

Keep welcome keys:

$my_cleaned_array = array_intersect_key($my_dirty_array, array_flip($array_of_whitelisted_keys));
This entry was posted in Coding, php and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s