CoderZone.org

Category: >> PHP Code >> Search array elements for a substring Bookmark and Share

<< lastnext >>

Snippet Name: Search array elements for a substring

Description: Searches all array elements for a given substring. This function will return an array of the found key=>value pairs, or FALSE if the substring was not found.

Also see:
» Find first non-numeric character i...
» Create an array from words in an H...
» Randomize an array
» Removing duplicate array items whi...
» Get every 2nd (or 3rd, 4th, ...) e...
» Grab the last element of an array
» Create an array from the contents ...
» Create an array from the lines of ...
» Create an array from comma separat...
» Create an array from a range of le...
» Easy Numeric Array Sorting
» Handy In_Array() Just Like In PHP
» Read GET URL Variables Into An Ass...
» Search and Replace On Every Field ...
» Array of all US states
» Create a drop down menu from an ar...
» Search All Words Of A String In my...
» Unset unkown array element
» SUBSTR (SubString)
» Parse RSS into array
» Quickly sort associative arrays
» Recursively traverse array
» Combining three arrays into one
» Combining two arrays into one
» Check if array is associative
» Update MySQL Table Using An Array
» Finding a substring in a string
» PHP to JavaScript Array
» Unique Array function

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

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 over  array to get the search pattern 
    foreach ($needle as $pattern)
    {
        if (count($found = preg_grep("/$pattern/", $haystack)) > 0) {
        	return $found;
        }
    }
 
    // if not found 
    return false;
}
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 7