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; } Quote Link to comment 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) { } Quote Link to comment 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'; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.