1

I have an array which gives me correct results when I print it, for example:

[0] => [email protected],
[1] => 0909,
[2] => [email protected],
[3] => 0909

Now, when I want to check if [email protected] is in the array it gives me an error that the value doesnt exist in this array, but when I try for example [email protected] it gives the correct result.

This is a little part of the code: $user is the word I want to search, $arrayname is the array.

if (array_search(strtolower($user),array_map('strtolower',$arrayname))){
//value exist
}
else{
//value does not exist
}

Now [email protected] doesn't exist it says, while [email protected] does exist.

Who has any idea?

2
  • 2
    The cause is actually in the documentation. "This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function." Commented Aug 1, 2013 at 14:59
  • Because it returns zero Commented Aug 2, 2013 at 6:00

2 Answers 2

4

array_search returns the index of the value that is found. When you search for the first item it returns 0. which is also means false. change your code so that it reads

if (false !== array_search(strtolower($user),array_map('strtolower',$arrayname))){

an alternative method would be to use in_array

if(in_array(strtolower($user),array_map('strtolower',$arrayname))){
Sign up to request clarification or add additional context in comments.

Comments

0

I simply use is_numeric

if (is_numeric(array_search(strtolower($user), $arrayname)) {
    /* do something */
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.