note 41887 added to function.in-array

From: Date: Sat, 24 Apr 2004 18:54:49 +0000
Subject: note 41887 added to function.in-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-68741@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/function.in-array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+41887 Delete -- http://master.php.net/manage/user-notes.php?action=delete+41887&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+41887&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#68741) next »