find needle w/ wildcard in an array


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



Copy this code and paste it in your HTML
  1. /**
  2.  * Takes a needle and haystack (just like in_array()) and does a wildcard search on it's values.
  3.  *
  4.  * @param string $string Needle to find
  5.  * @param array $array Haystack to look through
  6.  * @result array Returns the elements that the $string was found in
  7.  */
  8. function wildcard_in_array ($string, $array = array ())
  9. {
  10. foreach ($array as $key => $value) {
  11. unset ($array[$key]);
  12. if (strpos($value, $string) !== false) {
  13. $array[$key] = $value;
  14. }
  15. }
  16. return $array;
  17. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.