Remove an Item From an Array By Value
Saturday, May 9th, 2009
I was working on my CMS a couple days ago when I was trying to remove an item from an array by value. I tried to find a function already built into PHP but couldn't. I developed this little function to do this simple task for me.
How am I going to call this function?
I sometimes like to imagine how I'm going to call this function before I actually write it. I decided that having three parameters would be just fine for my situation; the third parameter, $preserve_keys, could be removed and this function would still work just like it should.
<?php
$myArray = array('item1', 'item2', 'item3' ...);
$removeThis = 'item2';
$newArray = remove_item_by_value($removeThis, $removeThis);
?>
Breaking it down one line at a time
Instead of writing one line at a time, I thought it would be better to give you the code then explain each one of the lines individually.
<?php
function remove_item_by_value($array, $val = '', $preserve_keys = true) {
if (empty($array) || !is_array($array)) return false;
if (!in_array($val, $array)) return $array;
foreach($array as $key => $value) {
if ($value == $val) unset($array[$key]);
}
return ($preserve_keys === true) ? $array : array_values($array);
}
?>
- Lines 1-2: We begin by opening up PHP and declaring the function. Three parameters are allowed to be passed into this function: the array we want to remove items from; the value we're looking for when we remove items; and finally if the function should preserve the keys of the stripped array (this is helpful for associate arrays).
- Lines 3-4: Some basic checking goes on before we loop through the array. The function will return false if the array is empty or it isn't even an array at all. Also, I added at the last minute a check to see if this value even exists in the array. There would be no point in wasting server time looping through an array looking for something that isn't even there.
- Lines 6-8: We use a foreach loop to check each value and see if it matches up with the value passed. If there is a match, the function unsets that specific item. PHP tip: To select the current item in a foreach loop make sure you assign the key to a variable then get it with a similar syntax to $array[$key].
- Line 10: We use a ternary operator to return the array with its keys intact or with new keys. An easy way to return only the values of an array is using array_values.
- Lines 11-12: We close the function and PHP and call this function done!
Conclusion
As you can see, this function is very simple but can save you loads of time down the road. However, I can already see a problem with this. It doesn't accommodate multi-demensional arrays. Don't be afraid to build off this function and have it work with multi-dementional arrays, I would love to see what you would do to solve that!
If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!
Article Sponsored by:
Expert guidance on designing a great theme for one of the most popular, open-source blog systems available for the Web today! This book can be used by WordPress users or visual designers (with no server-side scripting or programming experience) who are used to working with the common industry-standard tools like PhotoShop and Dreamweaver or other popular graphic, HTML, and text editors. Regardless of your web development skill-set or level, you'll be walked through the clear, step-by-step instructions, but familiarity with a broad range of web development skills and WordPress know-how will allow you to gain maximum benefit from this book.








An easier way would be
if(array_search(“value”,$array) !== false){
unset($array[array_search("value",$array)]);
}
Or if we’re reducing
if ($key = array_search('asd', $arr1)){
unset($arr1[$key]);
}
Sorry… to use the articles variables…
if ($key = array_search('value', $array)){
unset($array[$key]);
}
This also fails if ‘value’ is the zeroeth entry in $array.
I.e.
$array = array(‘value’, ’someothervalue’);
Will not be filtered to array(’someothervalue’), because zero evaluates as false.
This does not cover the same functionality as the original, since this will only remove the first instance of the value, not all instances. Assuming that the array can have the searched value more than once.
From http://us2.php.net/array_search:
If needle is found in haystack more than once, the first matching key is returned.
As Konr Ness said, array_search only finds the first instance of a value. You would need to use a while loop to find all of the instances.
I think preg_grep provides already this functionaity
Assuming the objective is not necessarily about finding the absolute quickest way, but rather providing a nice short function that performs the task with a few little options, this is great! The alternatives in the comments don’t give you the option of preserving keys or not depending on the situation (without writing different code everytime).
I would probably write it like this myself though:
function remove_item_by_value($val, $arr, $preserve = true) {if (empty($arr) || !is_array($arr)) { return false; }
while (in_array($val, $arr)) {
unset($arr[array_search($val, $arr)]);
}
return ($preserve ? $arr : array_values($arr));
}
But I realise this is much the same and possibly just about preferences
Depending on how complex you have things setup, you could use the value as you key and just give the value to that element as 1.
ie:
$arr = array(‘hasAdminRights’ => 1,
”canEditPosts’ => 1);
unset($arr['canEditPosts']);
For simple settings/options, yes, that would work fine. When you get into more complex things it would be a little hairy.
Yes it can get hairy when it gets complex. But the speed benefit is awesome thou. Just all about the needs.
Another solution
function remove_item_by_value($val, $arr, $preserve = true) {if (empty($arr) || !is_array($arr)) { return false; }
foreach(array_keys($arr,$val) as $key){ unset($arr[$key]); }
return ($preserve) ? $arr : array_values($arr);
}
Oh, just tested that one; nice use of array_keys!
I was about to say what’s already been said so just joining in…
I happened to write a similar function today and I used array_search and unset() and it works just fine and reduces the array.
-Alex
$array = array(“red”, “green”, “blue”, “yellow”);
if ($index = array_search(‘green’, $array) !== false) {
array_splice($array,$index,1);
}
print_r($array);
the reason I’m not using unset is that unset keeps old indexes and breaks for loops
“the reason I’m not using unset is that unset keeps old indexes and breaks for loops”
Ivan, it doesn’t keep old indices…
Just tested it again to be sure and it works as advertised and unsetting an element in the middle of the array shifts the indices accordingly…
-Alex
$array = array(“red”, “green”, “blue”, “yellow”);
unset($array[2]);
print_r($array);
output:
Array
(
[0] => red
[1] => green
[3] => yellow
)
as you can see index of yellow is still 3. and for loop with count will not work as expected
That is why the preserve_keys flag is added. Array_values rebuilds the array to get consecutive keys.
There are situations where consecutive keys aren’t necessary so why bother altering the keys.
array_search is good when the function has to remove the first value but in that case you better use array_unique.
if removing more than one item by value, why not use array_diff ?
Good work
[edited by admin. See below for properly formatted code]
00ps it’s filtering my code though i wrapped it into
tag$array = array('red', 'blue', 'green', 'white','red', 'blue', 'green', 'white');
print_r($array);
while (is_numeric($key = array_search('red', $array))) {
array_splice($array, $key, 1);
}
print_r($array);
quote: “An easy way to return only the keys of an array is using array_values.”
Should be “values”, not keys, right?
“An easy way to return only the values of an array is using array_values.”
Yeah, my fault. Thanks for catching that!
$newArray = remove_item_by_value($removeThis, $removeThis);
>>
$newArray = remove_item_by_value($myArray, $removeThis);
What about something like this:
error_reporting(E_ALL);
# remove by key:
function array_remove_key ()
{
$args = func_get_args();
if (empty($args[0]) || !is_array($args[0])) return false;
return array_diff_key($args[0],array_flip(array_slice($args,1)));
}
# remove by value:
function array_remove_value ()
{
$args = func_get_args();
if (empty($args[0]) || !is_array($args[0])) return false;
return array_diff($args[0],array_slice($args,1));
}
$myArray = array('item1', 'item2', 'item3','item1', 'item2', 'item3','...');
$myArray = array_remove_value($myArray,'item2');
echo '',print_r($myArray,TRUE),'';
$myArray = array('item1', 'item2', 'item3','item1', 'item2', 'item3','...');
$myArray = array_remove_value($myArray,'item1','item3');
echo '',print_r($myArray,TRUE),'';
And incidentally, there's an error on Line 3 of the second block of code in the article. Emptyempty isn't a function.
Sigh. With escaped entities this time.
error_reporting(E_ALL);
# remove by key:
function array_remove_key ()
{
$args = func_get_args();
if (empty($args[0]) || !is_array($args[0])) return false;
return array_diff_key($args[0],array_flip(array_slice($args,1)));
}
# remove by value:
function array_remove_value ()
{
$args = func_get_args();
if (empty($args[0]) || !is_array($args[0])) return false;
return array_diff($args[0],array_slice($args,1));
}
$myArray = array('item1', 'item2', 'item3','item1', 'item2', 'item3','...');
$myArray = array_remove_value($myArray,'item2');
echo '<pre>',print_r($myArray,TRUE),'</pre>';
$myArray = array('item1', 'item2', 'item3','item1', 'item2', 'item3','...');
$myArray = array_remove_value($myArray,'item1','item3');
echo '<pre>',print_r($myArray,TRUE),'</pre>';
Good
In PHP 5.3:
$array=array_filter($array, function($value)
{
if($value==”get rid of this”)
{
return false;
}
return true;
});
Hi,
And this is too simple ?
:
$search_array = array('gif', 'jpg', 'png');$item = 'png';
$rez= removeItemFromArray($search_array,$item);
var_dump($rez);
function removeItemFromArray($list, $item) {
return array_diff($list, (array)$item);
}
Fab
__fabrice hit the nail on the head. It doesn’t even need to incur the overhead of an additional function, it’s a one function call. I prefer writing it without casting. Also handles removing more than a single value at once and keeps indexes. Can reset indexes later if you want.
array_diff($orig_array, array($value_to_remove));
as described on php.net on preg_grep;
$nomatch = preg_grep(“/{$keyword}/i”,$array,PREG_GREP_INVERT);
$nomatch returns array without $keywords.
$array = array(0=>array('avatar_id'=>1,
'quantity'=>4),
1=>array('avatar_id'=>1,
'quantity'=>4),
2=>array('avatar_id'=>3,
'quantity'=>4));
function removeArrayKey($array,$toSearch,$innerSearchArrayKey){
$arrayCount = count($array);
if($arrayCount>0){
for($i=0;$i0)?$array:false;
}else{
return false;
}
}
function removeArrayKey($array,$toSearch,$innerSearchArrayKey){
$arrayCount = count($array);
if($arrayCount>0) {for($i=0;$i0)?$array:false; }else{ return false; } }