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

时间:2020-12-09 15:40:56

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?

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

7 个解决方案

#1


24  

You could just unset the value:

您可以取消设置值:

$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;
}

#2


11  

Use 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。

#3


10  

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 $input array having keys SomeKey1, SomeKey2 and SomeKey3 and return all others into $result variable.

这将从$ input数组中排除具有键SomeKey1,SomeKey2和SomeKey3的元素,并将所有其他元素返回到$ result变量中。

#4


1  

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

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

#5


1  

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.

这会稍快一点,如果您需要处理大型数据集,可能会有所帮助。

#6


0  

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

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

unset($array['key']); 

DEMO http://codepad.org/EA9vTwzR

#7


-2  

array_diff_assoc could help. So i.e. you could use

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

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

#1


24  

You could just unset the value:

您可以取消设置值:

$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;
}

#2


11  

Use 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。

#3


10  

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 $input array having keys SomeKey1, SomeKey2 and SomeKey3 and return all others into $result variable.

这将从$ input数组中排除具有键SomeKey1,SomeKey2和SomeKey3的元素,并将所有其他元素返回到$ result变量中。

#4


1  

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

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

#5


1  

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.

这会稍快一点,如果您需要处理大型数据集,可能会有所帮助。

#6


0  

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

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

unset($array['key']); 

DEMO http://codepad.org/EA9vTwzR

#7


-2  

array_diff_assoc could help. So i.e. you could use

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

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