note 29081 deleted from function.in-array by danbrown
| From: | danbrown@php.net | Date: | Mon, 08 Jun 2009 21:46:50 +0000 |
| Subject: | note 29081 deleted from function.in-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156528@lists.php.net to get a copy of this message | ||
Note Submitter: greg at serberus dot co dot uk
----
With in_array() you need to specify the exact value held in an array element for a successful match.
I needed a function where I could see if only part of an array element matched so I wrote this
function to do it, it also searches keys of the array if required (only useful for associative
arrays). This function is for use with strings:
function inarray($needle, $array, $searchKey = false)
{
if ($searchKey) {
foreach ($array as $key => $value)
if (stristr($key, $needle)) {
return true;
}
}
} else {
foreach ($array as $value)
if (stristr($value, $needle)) {
return true;
}
}
}
return false;
}