note 41887 deleted from function.in-array by danbrown
| From: | danbrown@php.net | Date: | Mon, 08 Jun 2009 21:46:25 +0000 |
| Subject: | note 41887 deleted from function.in-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156525@lists.php.net to get a copy of this message | ||
Note Submitter: php at NOSPAM dot fastercat dot com
----
The description of in_array() is a little misleading. If needle is an array, in_array() and
array_search() do not search in haystack for the _values_ contained in needle, but rather the array
needle as a whole.
$needle = array(1234, 5678, 3829);
$haystack = array(3829, 20932, 1234);
in_array($needle, $haystack);
--> returns false because the array $needle is not present in $haystack.
Often people suggest looping through the array $needle and using in_array on each element, but in
many situations you can use array_intersect() to get the same effect (as noted by others on the
array_intersect() page):
array_intersect($needle, $haystack);
--> returns array(1234). It would return an empty array if none of the values in $needle were
present in $haystack. This works with associative arrays as well.