FangXIII
FangXIII

Reputation: 81

How do i search an array in php and return true if it finds one of my variable?

if (array_search($soca, $user_socs))

I know that this will search the array by inputing a variable and an array but i was wondering if it is possible to make it return true, because since it doesn't return true, my if statement is not being carried out.

References

Upvotes: 2

Views: 4297

Answers (2)

Mohit Jain
Mohit Jain

Reputation: 30489

if (($key = array_search($soca, $user_socs)) !== FALSE) {
  ...
}

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94672

You need to code this function to expect a return of FALSE if it does not find what you are looking for.

So

if (array_search($soca, $user_socs) !== false) {
    // then I found something
    // not sure what but its defintely not 
    // returning a failed to find situation
} else {
    // I did not find anything
}

Note the use of the !== and NOT != this is expressly for situations where a function can return actual data that might equate to false but not actually be false i.e. where it may return the 0'th occurance its a valid occurance which may be confused with false if you just us ==.

Upvotes: 6

Related Questions