Reputation: 20592
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?
Upvotes: 42
Views: 87090
Reputation: 2013
if you wanted Laravel way, The Arr::except method removes the given key / value pairs from an array:
use Illuminate\Support\Arr;
$array = ['name' => 'Desk', 'price' => 100];
$filtered = Arr::except($array, ['price']);
// ['name' => 'Desk']
@source https://laravel.com/docs/8.x/helpers#method-array-except
Upvotes: 16
Reputation: 953
Simple function here, using two arrays, the actual array and an array of keys that should be excluded. Could also easily be made into a one liner if we exclude the function.
function array_except(array $array,array $except) {
return array_filter($array,fn($key) => !in_array($key,$except),ARRAY_FILTER_USE_KEY);
}
Upvotes: 3
Reputation: 6710
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.
Upvotes: 60
Reputation: 2923
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 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.
Upvotes: 1
Reputation: 9335
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.
Upvotes: 17
Reputation: 369
array_diff_assoc could help. So i.e. you could use
$onlyBeta = array_diff_assoc($array, array('alpha', 'gamma'))
Upvotes: -2
Reputation: 816482
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;
}
Upvotes: 45
Reputation: 29160
You can easily remove an array item by its key using this..
unset($array['key']);
DEMO http://codepad.org/EA9vTwzR
Upvotes: 0
Reputation: 11882
$alphaAndGamma = $array;
unset($alphaAndGamma['alpha']);
$onlyBeta = $array;
unset($onlyBeta['alpha'], $onlyBeta['gamma']);
Upvotes: 2