note 15841 deleted from function.in-array by jimw
| From: | jimw@php.net | Date: | Fri, 07 Dec 2001 21:08:32 +0000 |
| Subject: | note 15841 deleted from function.in-array by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-21980@lists.php.net to get a copy of this message | ||
My above example gives false positives if elements of $needle are in $haystack, even if $needle
doesn't appear in haystack in its entirety. Also it can't use scalars as $needle. Back to
the drawing board...
Here's an inside_array that will succeed if exactly $needle is anywhere within an array
$haystack of any number of dimensions, where $needle is any type.
function inside_array($needle, $haystack)
{
if ($needle === $haystack) return true;
if (is_array($haystack))
{
foreach($haystack as $haystack_element)
{
if ($needle === $haystack_element)
{
return true;
}
else
{
if (inside_array($needle, $haystack_element)) return true;
}
}
}
else
{
return false;
}
}