PHP function any_in_array - tests if any of $needles are in $haystack

Richard's picture

Just a quick post. You might find this useful. It compares one array ($needles) against another ($haystack) and returns true if any element of $needles is present in $haystack

/**
 * alternative in_array(). Tests if one or more of $needle are in $haystack
 * 
 * e.g.
 *   echo any_in_array(array("a", "x", "z"), array("a", "b", "c")); 
 *   returns true ("a" is in $haystack)
 * 
 * @param $needles	array	things to search for
 * @param $haystack array	thing to search in
 * 
 * @return true if any of $needles in $haystack
 */
function any_in_array($needles = array(), $haystack = array()) {
 
	foreach($needles as $needle) {
 
		if (in_array($needle, $haystack)) {
 
			return true;
 
		}
 
	}
 
	return false;
 
}
  • Richard@Home:

Comments

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options