Return to Snippet

Revision: 23011
at January 27, 2010 16:56 by lancemonotone


Initial Code
/**
 *
 * @Search for substring in an array
 *
 * @param string $neele
 *
 * @param mixed $haystack
 *
 * @return mixed Array or false if not found
 *
 */
function substr_in_array($haystack, $needle)
{
  $found = array();
	/*** cast to array ***/
    $needle = (array) $needle;

    /*** map with preg_quote ***/
    $needle = array_map('preg_quote', $needle);

    /*** loop of the array to get the search pattern ***/
    foreach ($needle as $pattern)
    {
        if (count($found = preg_grep("/$pattern/", $haystack)) > 0) {
        	return $found;
        }
    }
    /*** if it is not found ***/
    return false;
}

Initial URL
http://368design.com

Initial Description
Searches all array elements for a given substring.  Returns an array of found key=>value pairs or false if not found.

Initial Title
substr_in_array()

Initial Tags
search, array, find

Initial Language
PHP