PHP: Remove Values from Array

If you need to remove elements from an array that have a particular value(s) here’s a neat way of doing it without any looping:

PHP:
  1. // our initial array
  2. $arr = Array("blue", "green", "red", "yellow", "green", "orange", "yellow", "indigo", "red");
  3. print_r($arr);
  4.  
  5. // remove the elements who's values are yellow or red
  6. $arr = array_diff($arr, array("yellow", "red"));
  7. print_r($arr);
  8.  
  9. // optionally you could reindex the array
  10. $arr = array_values($arr);
  11. print_r($arr);

This is the output from the code above:
Array ( [0] => blue [1] => green [2] => red [3] => yellow [4] => green [5] => orange [6] => yellow [7] => indigo [8] => red )
Array ( [0] => blue [1] => green [4] => green [5] => orange [7] => indigo )
Array ( [0] => blue [1] => green [2] => green [3] => orange [4] => indigo )

Pete Graham xXx

Comments 13

  1. Laptoptips wrote:

    Great tip, thanks.
    One suggestion: array_values() will reindex a numerical array nicely, but will remove all key strings from the array and replace them with numbers. If you need to preserve the key names (strings), or reindex the array if all keys are numerical, use array_merge(), maybe even:
    $arr = array_merge(array_diff($arr, array("yellow", "red")));

    Posted 15 May 2007 at 8:39 pm
  2. Pete wrote:

    I’ve tested this and it works nicely for arrays with mixed indexes (strings and numeric), good tip yourself!

    Posted 16 May 2007 at 8:11 am
  3. mgwalk wrote:

    I was trying to use the unset function but now after reading this i am going to try the array_diff function.

    Posted 09 Mar 2008 at 3:54 am
  4. TaSwavo wrote:

    I used used this even though I wanted to remove only one user selected value.

    $arr = array_diff( $arr, array(“$removal”) );

    Works very well. Thanks for thinking to post it.

    Posted 29 May 2008 at 9:08 pm
  5. Maarten wrote:

    Thanks, lots better than a loop. You are first hit on “Remove Values from Array” :)

    Posted 01 Jun 2008 at 10:48 pm
  6. Bert wrote:

    Thanks! First hit on Google “php remove a value in array”! ;)

    Posted 06 Jun 2008 at 1:28 pm
  7. PFWD wrote:

    Great tip. Helped me out on a project of mine.

    Posted 29 Jun 2008 at 6:01 pm
  8. Chad wrote:

    I have been looking for a better way to do this (without defining a function). Thanks

    Posted 25 Jul 2008 at 4:46 am
  9. Amit Palshikar wrote:

    Thats a great tip. Thanks a lot!

    Posted 27 Aug 2008 at 2:03 pm
  10. Michael wrote:

    Nice tip… thanks!

    BTW, your the first hit for “php delete value in array”, also.

    Posted 15 Dec 2008 at 2:22 pm
  11. Marion wrote:

    “I was trying to use the unset function but now after reading this i am going to try the array_diff function.” — same here, and this worked — thanks!

    Posted 26 Jan 2009 at 6:41 pm
  12. DEVTRENCH wrote:

    Many thanks for posting this code snippet. It helped me out and it works well.

    Posted 31 Jul 2009 at 7:14 pm
  13. Carl wrote:

    Thank you for this succinct bit of code. It was exactly what I was looking for.

    Posted 15 Aug 2010 at 7:13 pm

Trackbacks & Pingbacks 1

  1. From PFWD : blog.peterfisher.me.uk : Remove array values without any loops : on 29 Jun 2008 at 7:59 pm

    [...] I found an array_diff example on Peter Grahams blog while I was writing a simple site configuration manager in cakePHP. I needed to reduce an array to certain values based on a passed condition. Peter Grahams example showed me exactly what I need. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *