note 19564 deleted from function.array-search by didou

From: Date: Tue, 16 Dec 2003 09:32:48 +0000
Subject: note 19564 deleted from function.array-search by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-62031@lists.php.net to get a copy of this message
Note Submitter: dakota@dir.bg ---- Do not expect array_search() to return TRUE or FALSE like in_array(). if ($id = array_search($needle, $array)) ... Note that if the corresponding key of the value is 0 (the first element of an array), the value is found, but the function returns 0. So, use isset() for the given key like in the following example: $myarray = array(4, 8, 10); $array2check = array(4, 9); foreach ($array2check as $k=>$v) { $id = array_search($v, $myarray); if (isset($id)) { echo "$v found: \$myarray[$id]\n"; } } $id is undefined if array_search() returns FALSE, so you don't have to unset it at the end of the foreach() cycle. P.S.: The example above can be used instead of array_diff() which was broken, I don't know the reasons why.

« previous php.notes (#62031) next »