note 38983 added to function.in-array
| From: | "FrankStr�ter" at rn2 dot php dot net | Date: | Wed, 14 Jan 2004 03:34:51 +0000 |
| Subject: | note 38983 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-63488@lists.php.net to get a copy of this message | ||
I combined some ideas in previous notes to this elegant function for checking against values in
multi-dimensional arrays.
It also solves the bug mentioned by Raphinou, which is 'If the haystack contains the boolean
true, in_array returns true!!'.
bool in_array_multi ( mixed needle, array haystack )
<?php
function in_array_multi($needle, $haystack) {
static $return = false;
if (!is_array($haystack)) return false;
while (list($key, $value) = each($haystack)) {
if ($value == $needle) {
$return = true;
break;
}
if (is_array($value))
in_array_multi($needle, $value);
}
return $return;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38983
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38983&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38983&report=yes
Search -- http://master.php.net/manage/user-notes.php