array remove value


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.