note 54332 deleted from function.in-array by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 18:57:32 +0000 |
| Subject: | note 54332 deleted from function.in-array by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134483@lists.php.net to get a copy of this message | ||
Note Submitter: alex at alexelectronics dot com
----
A foreach would be simpler for a multi-dimensional array search (and probably faster, since you an
use the value instead of the index). Like this:
<?PHP
function in_multi_array($needle, $haystack) {
$in_multi_array = false;
if(in_array($needle, $haystack)) {
$in_multi_array = true;
} else {
foreach $haystack as ($key => $val) {
if(is_array($val)) {
if(in_multi_array($needle, $val)) {
$in_multi_array = true;
break;
}
}
}
}
return $in_multi_array;
}
?>
Using is_array($val) is probably faster than using is_array($haystack[$key])