Posted By

DADU on 09/04/10


Tagged

array remove all


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

DADU
mikael12
mmcachran


array remove value


 / Published in: PHP
 

This PHP code snippet searches a value in an array. Depending on use, one or all matching values and their belonged keys are removed.

  1. /* EXAMPLE
  2.  
  3. print_r(array_remove(array('d','a','d','u'),'d',true));
  4.  
  5. */
  6.  
  7. function array_remove($array,$value,$remove_all=false) {
  8.  
  9. $keys = array_keys($array,$value);
  10.  
  11. switch($remove_all):
  12.  
  13. case false:
  14. $key = (count($keys) >= 1 ? $keys[0] : NULL);
  15. unset($array[$key]);
  16. break;
  17.  
  18. case true:
  19. foreach($keys as $key) unset($array[$key]);
  20. break;
  21.  
  22. endswitch;
  23.  
  24. return $array;
  25. }

Report this snippet  

You need to login to post a comment.