Josh Schumacher: A PHP Developer

The biggest and the best in the Northwest

Josh Schumacher: A PHP Developer random header image

PHP - Delete selected items from an array

March 22nd, 2007 · 11 Comments

I was enlightened the day I discovered array_filter. You may find yourself with an array of values and you want to delete some of those values from the array. There is no 'delete element from array' function built into php but you can use an even more powerful function, array_filter.

Let's look at a simple example. I have an array containing values, some are numeric, some might not be. I only want my array to contain the valid numeric entries and I want to delete the other values. PHP has a helper function we are going to be utilizing, is_numeric.

PHP:
  1. $data = array(42, "foo", 96, "", 100.96, "php");
  2.  
  3. // Filter each value of $data through the function is_numeric
  4. $numeric_data = array_filter($data, "is_numeric");
  5.  
  6. // Re-key the array so the keys are sequential and numeric (starting at 0)
  7. $numeric_data = array_values($numeric_data);
  8.  
  9. // Print out the new, filtered data
  10. print_r($numeric_data);

The output from above is:

Array
(
    [0] => 42
    [1] => 96
    [2] => 100.96
)

Pretty handy function. Notice how I ran my data through the function array_values, before that function was run, the keys were (0,2,4).

Another example with a user defined function. I have defined a collection of tags and I want my tag collection to only contain tags that match the criteria the user has requested.

PHP:
  1. $tagCollection = array("AJAX", "Analytics", "CSS", "Javascript", "JoshSchumacher.com", "PHP", "Prototype", "Web 2.0");
  2.  
  3. // Custom function used to filter the $tagCollection based on query sent to page
  4. function strTagMatch($val)
  5. {
  6.   $query = isset($_REQUEST['Tags']) ? $_REQUEST['Tags'] : '';
  7.   return (stripos($val, $query) !== false);
  8. }
  9.  
  10. $tags = array_filter($tagCollection, "strTagMatch");

Voila! Deleting elements from an array has never been easier.

For more information, see the php documentation on array_filter.

Tags: PHP

11 responses so far ↓

  • 1 evll // May 11, 2007 at 1:08 am

    Actually, instead of using user defined function, you could use array_diff().

    For example: array_diff( array(’a', ‘b’), array(’b') ) would delete ‘b’ in much easier way…

  • 2 Josh.Schumacher // May 11, 2007 at 5:44 am

    Correct, you could use array_diff() to delete an element if you know what the elements are that you are deleting. I chose to use an array_filter() though because you can do conditional deleting. For example, you can delete it only if it’s not numeric or if it’s greater than 5 characters.

    You can’t perform conditional deletes using an array_diff() but it is a simple way to delete items you do know the value.. Thanks for another quick way to delete an item from an array in php.

  • 3 Firass // Jul 20, 2007 at 3:31 pm

    Dude, you need to blog more.

  • 4 Henke // Sep 12, 2007 at 7:40 am

    The arrat_diff function is perfelct for what i.m working on now since it has no objects in the the arrays .

    Thanks for the tip.

  • 5 Mat?ss // Nov 13, 2007 at 5:21 am

    And what about array with keys not indexes?

    array (’key1′ => ‘value1′, ‘key2′ => ‘value2′);

    how can you delete an item by key not value?

  • 6 Mat?ss // Nov 13, 2007 at 5:23 am

    answer to my own question. array_diff_key i guess..

  • 7 Josh.Schumacher // Nov 13, 2007 at 8:39 am

    If you know exactly which key in an array you want to delete, use unset

    $a = array(’foo’=>8, ‘bar’=>3245, ‘foobar’=>213);

    To make the array only contain keys ‘foo’ and ‘bar’ just:
    unset($a['foobar']);

  • 8 Salah // Jan 7, 2008 at 4:07 am

    Matiss ,

    The last solution given by Josh is valid. However it has the same problem as array_diff() versus array_filter() in that you have to know the keys in advance.

    To go past that shortcoming, you can use “foreach”.

    foreach ($array as $key => $value) {
    if ($array[$key] == ”’) unset($array[$key]);
    }

    Here we deleted all empty values from the array. But you can specify any condition you want.

  • 9 Mat?ss // Feb 21, 2008 at 11:25 pm

    ok, thx a lot for solutions!

  • 10 Nurul // Mar 6, 2008 at 8:58 am

    Thanks evll , For the quickest solution.

  • 11 mgwalk // Mar 8, 2008 at 8:40 pm

    Nice post but i think the unset function works better for my use!

    Thanks

Leave a Comment