note 12049 added to function.array-search.php
| From: | kerry at lothrop dot de | Date: | Tue, 20 Mar 2001 10:24:22 +0000 |
| Subject: | note 12049 added to function.array-search.php | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-12581@lists.php.net to get a copy of this message | ||
If you want one that will also work for an associative array, try this:
function array_search ($needle, $haystack, $strict = FALSE) {
foreach (array_keys($haystack) as $key) {
if ($strict) {
if ($haystack[$key] === $needle) return $key;
}
else {
if ($haystack[$key] == $needle) return $key;
}
}
return FALSE;
}
Note the function array_keys also has functionality for finding the key for a specified element.
http://www.php.net/manual/en/function.array-search.php