63

I have an associative array of data and I have an array of keys I would like to remove from that array (while keeping the remaining keys in original order -- not that this is likely to be a constraint).

I am looking for a one liner of php to do this.
I already know how I could loop through the arrays but it seems there should be some array_map with unset or array_filter solution just outside of my grasp.

I have searched around for a bit but found nothing too concise.

To be clear this is the problem to do in one line:

//have this example associative array of data
$data = array(
    'blue'   => 43,
    'red'    => 87,
    'purple' => 130,
    'green'  => 12,
    'yellow' => 31
);

//and this array of keys to remove
$bad_keys = array(
    'purple',
    'yellow'
);

//some one liner here and then $data will only have the keys blue, red, green
3

4 Answers 4

154
$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).

Sign up to request clarification or add additional context in comments.

2 Comments

I had to think about this one for a mo. The point is $bad_keys = array(0=>'purple',1=>'yellow') so the keys in the array_flip are 'purple','yellow'. Hence the diff_key works. Neat. Thanks Kolink.
I once needed to remove by value (not keys) and I ended up with this: $myArr = array(5, 25, 5, 30); ... THEN ... $myArr =& array_diff_key($myArr, array_flip(array_keys($myArr, 5)));
39

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.

1 Comment

This answer has been posted on the wrong page; it doesn't do what this page is asking for. It is suitable (and already posted) on this earlier page: How do I remove keys from an array which are not in another array?
0

This is a blacklisting function I created for associative arrays.

if(!function_exists('array_blacklist_assoc')){

    /**
     * Returns an array containing all the entries from array1 whose keys are not present in any of the other arrays when using their values as keys.
     * @param array $array1 The array to compare from
     * @param array $array2 The array to compare against
     * @return array $array2,... More arrays to compare against
     */

    function array_blacklist_assoc(Array $array1, Array $array2) {
        if(func_num_args() > 2){
            $args = func_get_args();
            array_shift($args);
            $array2 = call_user_func_array('array_merge', $args);
        } 
        return array_diff_key($array1, array_flip($array2));
    }
}

$sanitized_data = array_blacklist_assoc($data, $bad_keys);

1 Comment

-1

array_filter() is more readable, in my opinion.

Here's how I'd do it:

$data = array_filter($data, function($k) use ($bad_keys) {return !in_array($k, $bad_keys);}, ARRAY_FILTER_USE_KEY);

1 Comment

PHP consistently performs worse when making "values" checks (e.g. in_array(), array_search()) versus making "key" checks (e.g. array_intersect_key(), isset(), key_exists()). This answer is not likely to outperform other, earlier answers on this page. This answer will make N calls of in_array(), but other answers on this page do not make iterated function calls. A Stack Overflow answer must never contain Hope this helps -- it is irrelevant to the page; no signoffs or esignatures. Modern concise code: $data = array_filter($data, fn($k) => !in_array($k, $bad_keys), ARRAY_FILTER_USE_KEY);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.