note 3911 deleted from function.in-array by jimw

From: Date: Fri, 07 Dec 2001 21:05:58 +0000
Subject: note 3911 deleted from function.in-array by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-21957@lists.php.net to get a copy of this message
It appears that if you have an array of numbers, one element of which is zero, and you pass in_array a string to compare to the values in that array, all string values will equate with the "0" element. For instance: $arr1 = array(0, 1, 2, 3, 4); in_array("asdf", $arr1) == True if you iterate through the array manually, "asdf" will equate with the "0" element. If you write your own in_array, and compare each element to the needle using strcmp, it appears to work properly. i.e.: function in_str_array($needle, $haystack) { reset($haystack); while( list($key, $val) = each($haystack) ) { if( !strcmp($val, $needle) ) { return true; } } return false; }

« previous php.notes (#21957) next »