Php array_search returns 0 for the first element?

I have this code

$restaurant = array('/restaurant_pos/', '/bar_nightclub_pos/') $current_page = $_SERVER['REQUEST_URI']; if (array_search($current_page, $restaurant)) { echo "KEEP ME"; } 

the problem is that array_search returns 0 because '/ restaurant_pos /' is the first element in the array that causes an if failure ... any ideas on how to check if the value is in the array without failing the first element

+8
arrays php
source share
3 answers
 if (array_search($current_page, $restaurant) !== FALSE ) { echo "KEEP ME"; } 

Manual link: http://php.net/manual/en/function.array-search.php

+24
source share

I think it is better to use in_array() in this case

 $restaurant = array('/restaurant_pos/', '/bar_nightclub_pos/') $current_page = $_SERVER['REQUEST_URI']; if (in_array($current_page, $restaurant)) { echo "KEEP ME"; } 

http://php.net/manual/en/function.in-array.php

+2
source share

From my own experience, if you got, for example:

 Array ( [1] => Array ( [0] => a [1] => b ) [2] => Array ( [0] => b ) [4] => Array ( [0] => c ) ) 

In array_search("a", $array[3]) !== FALSE) it returns TRUE the same, therefore, to cover all cases, also in the null element, it is better to use:

 if ( (array_search($element, $array) !== FALSE) && ($array) ) { echo "found"; } 

Hope this helps.

0
source share

All Articles