note 15841 added to function.in-array

From: Date: Thu, 04 Oct 2001 18:26:57 +0000
Subject: note 15841 added to function.in-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-18581@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; } } -- http://www.php.net/manual/en/function.in-array.php http://master.php.net/manage/user-notes.php?action=edit+15841 http://master.php.net/manage/user-notes.php?action=delete+15841 http://master.php.net/manage/user-notes.php?action=reject+15841

« previous php.notes (#18581) next »