php 返回除给定键之外的所有数组元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5147691/
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-25 16:56:38  来源:igfitidea点击:

Return all array elements except for a given key

phpassociative-array

提问by Dan Lugg

Simple one, I was just wondering if there is a clean and eloquent way of returning all values from an associative array that do not match a given key(s)?

简单的一个,我只是想知道是否有一种干净而雄辩的方法可以从与给定键不匹配的关联数组中返回所有值?

$array = array('alpha' => 'apple', 'beta' => 'banana', 'gamma' => 'guava');

$alphaAndGamma = arrayExclude($array, array('alpha'));
$onlyBeta      = arrayExclude($array, array('alpha', 'gamma'));

function arrayExclude($array, Array $excludeKeys){
    foreach($array as $key => $value){
        if(!in_array($key, $excludeKeys)){
            $return[$key] = $value;
        }
    }
    return $return;
}

This is what I'm (going to be) using, however, are there cleaner implementations, something I missed in the manual perhaps?

这就是我(将要)使用的,但是,是否有更干净的实现,我可能在手册中遗漏了什么?

回答by Felix Kling

You could just unsetthe value:

你可以只是unset价值:

$alphaAndGamma = $array;
unset($alphaAndGamma['alpha']);

Edit: Made it clearer. You can copy an array by assigning it to another variable.

编辑:说得更清楚了。您可以通过将数组分配给另一个变量来复制数组。

or in a function:

或在函数中:

function arrayExclude($array, Array $excludeKeys){
    foreach($excludeKeys as $key){
        unset($array[$key]);
    }
    return $array;
}

回答by Dev

Although, this question is too old and there are several answer are there for this question, but I am posting a solution that might be useful to someone.

虽然,这个问题太老了,这个问题有几个答案,但我发布了一个可能对某人有用的解决方案。

You may get the all array elements from provided input except the certain keys you've defined to exclude using:

您可以从提供的输入中获取所有数组元素,除了您定义为排除使用的某些键之外:

$result = array_diff_key($input, array_flip(["SomeKey1", "SomeKey2", "SomeKey3"]));

This will exclude the elements from $inputarray having keys SomeKey1, SomeKey2and SomeKey3and return all others into $resultvariable.

这将从$input具有 keys 的数组中排除元素SomeKey1SomeKey2并将SomeKey3所有其他元素返回到$result变量中。

回答by linepogl

Use array_diff_key():

使用array_diff_key()

$array = array('alpha' => 'apple', 'beta' => 'banana', 'gamma' => 'guava');

$alphaAndGamma = array_diff_key($array, array('alpha'=>0));
$onlyBeta      = array_diff_key($array, array('alpha'=>0, 'gamma'=>0));

EDIT: I added =>0s.

编辑:我添加了 =>0s。

回答by steveo225

$alphaAndGamma = $array;
unset($alphaAndGamma['alpha']);

$onlyBeta = $array;
unset($onlyBeta['alpha'], $onlyBeta['gamma']);

回答by dmcnelis

There have been a few discussions about speed when using in_array. From what I've read, including this comment1, using isset is faster than in_array.

关于使用 in_array 时的速度,有一些讨论。从我读过的内容来看,包括这条评论1,使用 isset 比 in_array 快。

In that case your code would be:

在这种情况下,您的代码将是:

function arrayExclude($array, array $excludeKeys){

    $return = [];

    foreach($array as $key => $value){
        if(!isset($excludeKeys[$key])){
            $return[$key] = $value;
        }
    }
    return $return;
}

That would be slightly faster, and may help in the event that you're needing to process large datasets.

这会稍微快一点,并且在您需要处理大型数据集的情况下可能会有所帮助。

回答by Dutchie432

You can easily remove an array item by its key using this..

您可以使用此键轻松删除数组项。

unset($array['key']); 

DEMOhttp://codepad.org/EA9vTwzR

演示http://codepad.org/EA9vTwzR

回答by frabala

array_diff_assoccould help. So i.e. you could use

array_diff_assoc可以提供帮助。所以即你可以使用

$onlyBeta = array_diff_assoc($array, array('alpha', 'gamma'))