Destramic Posted June 22, 2011 Share Posted June 22, 2011 ive tried this simple script and its not coming true...for some reason it doesnt search the first key if anyone can help please? $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); $key = array_search('blue', $array); if ($key) { echo hi; } Link to comment https://forums.phpfreaks.com/topic/240159-array-search-not-working/ Share on other sites More sharing options...
Alex Posted June 22, 2011 Share Posted June 22, 2011 array_search returns the key, so in your case it's returning 0, which evaluates to false. You need to use !== operator. if($key !== false) { } Link to comment https://forums.phpfreaks.com/topic/240159-array-search-not-working/#findComment-1233590 Share on other sites More sharing options...
priti Posted June 23, 2011 Share Posted June 23, 2011 Or you can use in_array() function which will return true/false. if(in_array('blue',array)) { echo 'hi'; } Link to comment https://forums.phpfreaks.com/topic/240159-array-search-not-working/#findComment-1233737 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.