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);
}
?>
  1. 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).
  2. 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.
  3. 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].
  4. 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.
  5. 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!

ABOUT THIS AUTHOR

Hey, I'm Vasili and I run duove.com. I love to code with PHP, MySQL, and jQuery, along with the basics like XHTML and CSS. At the moment, I just write (PHP) scripts for myself - like my CMS - but I hope that I can soon start freelancing for some people. Don't forget to follow me on Twitter for little tips and tricks I might tweet about. :)
  1. May 10, 2009 at 1:39 pm
    • Tom
      May 10, 2009 at 2:09 pm
      • Tom
        May 10, 2009 at 2:10 pm
      • Gherald
        March 2, 2010 at 6:37 pm
    • Konr Ness
      May 13, 2009 at 3:36 pm
    • May 13, 2009 at 4:16 pm
  2. PERL Guy
    May 10, 2009 at 1:54 pm
  3. May 10, 2009 at 7:08 pm
  4. EllisGL
    May 10, 2009 at 7:36 pm
    • May 13, 2009 at 4:07 pm
      • EllisGL
        May 13, 2009 at 9:39 pm
  5. david
    May 11, 2009 at 12:35 am
    • May 13, 2009 at 4:06 pm
  6. Alex
    May 11, 2009 at 6:08 pm
  7. May 11, 2009 at 10:27 pm
    • Alex
      May 12, 2009 at 1:55 pm
      • May 12, 2009 at 11:29 pm
    • david
      May 12, 2009 at 11:46 pm
  8. Teslegch
    May 19, 2009 at 1:40 am
  9. May 19, 2009 at 2:56 am
  10. May 31, 2009 at 12:38 pm
  11. May 31, 2009 at 12:43 pm
  12. christoph
    June 5, 2009 at 11:41 am
    • June 5, 2009 at 1:32 pm
  13. mrtnclzd
    June 18, 2009 at 7:56 pm
  14. July 31, 2009 at 6:10 am
    • July 31, 2009 at 6:12 am
  15. Chetan
    September 3, 2009 at 1:55 am
  16. Dan
    November 20, 2009 at 8:40 am
  17. __fabrice
    December 17, 2009 at 7:55 am
  18. Sounds
    December 18, 2009 at 2:39 pm
  19. simen
    January 13, 2010 at 9:09 pm
  20. January 17, 2010 at 5:09 am
    • Danish Jamil
      January 17, 2010 at 5:15 am

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates