11

It seems that that you can not use the search_array function in PHP to search the 0 index AND have it evalute as true.

Consider this code for example:

$test=array(100, 101, 102, 103);

if($key=array_search(100,$test)){

     echo $key;

}

else{

     echo "Not found";

} 

The needle '100' is found in the haystack and the key is returned as 0. So far so good, but then when I evaluate whether the search was successful or not it fails because the returned value is 0, equal to false!

The php manual suggests using '!==' but by doing so the key (array index) is not returned, instead either 1 or 0 is returned:

if($key=(array_search(103,$test)!== false)){

}

So how can I successfully search the array, find a match in the 0 index and have it evaluate as true?

user229044
  • 232,980
  • 40
  • 330
  • 338
  • Did you not see the [BIG RED BOX](http://php.net/array_search#refsect1-function.array-search-returnvalues)? Try to read the documentation and understand the language before you ask such questions. – cmbuckley Apr 10 '13 at 19:16

6 Answers6

44

This is explicitly mentioned in the docs. You need to use === or !==:

$key = array_search(...);

if ($key !== false) ...

Otherwise, when $key is 0, which evaluates to false when tested as a boolean.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • 2
    You could of course use `if( ($key = array_search(...)) !== false)` – Niet the Dark Absol Apr 10 '13 at 19:15
  • I read several threads on this, read the manual over and over but couldn't find the answer. I think I had the '!==' in the wrong part of my code. Now working - thanks! – user2056238 Apr 10 '13 at 19:16
  • but in this manner if I use code like if(array_search(...) === true), then why it is not working. (=== true is also string comparison) can anybody explain me – Ashish Rawat Jan 30 '23 at 10:03
  • @EstailSe Your code makes no sense. `array_search` can never return boolean `true`, so no, it will not work, and it's not clear what you're trying to accomplish. – user229044 Jan 30 '23 at 12:30
  • okay I got your point! thanks, I was comparing index number with Boolean, there I was wrong! – Ashish Rawat Feb 01 '23 at 09:00
5

The conditional in your second example block gives execution order priority to the !== operator, you want to do the opposite though.

if (($key = array_search(100,$test)) !== false) {

!== has higher precedence than == which makes the parentheses necessary.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • 1
    Note: This is the most correct in here. If I could transfer the upvotes from my awful, old, naive, now-deleted answer I would. – Sammitch Dec 06 '17 at 19:59
1
$key = array_search($what, $array);
if($key !== false and $array[$key] == $what) {
 return true;
}

it's more secure

Andrej Bestuzhev
  • 674
  • 6
  • 10
0
if(($key = array_search(103,$test)) !== false){

}
Peter Kiss
  • 9,309
  • 2
  • 23
  • 38
0
$test=array(100, 101, 102, 103);

if (($key = array_search(100,$test)) === false) {
    echo "Not found";
} else{
    echo $key;
} 
rorra
  • 9,593
  • 3
  • 39
  • 61
-1

Important: array_search has a 'STRICT' 3rd parameter to enforce a better search for difficult things like differ '0' from '00' but the point here is that:

The right test to confirm NOT FOUND is === or !== FALSE.

Some other functions in php you must compare the result with FALSE (===) to have a secure "not found". FALSE is equivalent to -1 from other languages (js/java)

if( strpos('Haystack', 'Hay') === FALSE){  // it is index 0 too...
   die('Not found');
} else {
   echo 'It is the string index ( 0 > -1 and !== FALSE )';
}
Sergio Abreu
  • 2,686
  • 25
  • 20