从 PHP 中的数组中删除黑名单键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11026799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:36:10  来源:igfitidea点击:

Remove blacklist keys from array in PHP

phparrayskey

提问by hackartist

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 linerof php to do this.
I already know how I could loop through the arrays but it seems there should be some array_mapwith unsetor array_filtersolution just outside of my grasp.

我正在寻找一种单行php 来做到这一点。
我已经知道我怎么会通过数组循环,但它似乎应该有一些array_mapunsetarray_filter解决方案外面我的把握。

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

回答by Niet the Dark Absol

$out =array_diff_key($data,array_flip($bad_keys));

$out =array_diff_key($data,array_flip($bad_keys));

All I did was look through the list of Array functionsuntil I found the one I needed (_diff_key).

我所做的只是查看Array 函数列表,直到找到我需要的函数( _diff_key)。

回答by Pmpr

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 whitelistinstead of a blacklist:

解决办法确实是黑暗阿布索涅特提供的解决方案。我想为任何追求类似事情的人提供另一种类似的解决方案,但这个解决方案使用白名单而不是黑名单

$whitelist = array( 'good_key1', 'good_key2', ... );
$output = array_intersect_key( $data, array_flip( $whitelist ) );

Which will preserve keys from $whitelistarray and remove the rest.

这将保留$whitelist数组中的键并删除其余键。

回答by TarranJones

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);