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 the array_search is returning 0 because '/restaurant_pos/' is the first element in the array which is causing the if to fail...any ideas on how to check if the value is in the array without failing on the first element
if (array_search($current_page, $restaurant) !== FALSE ) {
echo "KEEP ME";
}
Manual link: http://php.net/manual/en/function.array-search.php
I think it will be 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
I have used is_numeric function to achieve the same.
if(is_numeric(array_search($entry, $array))){
//if true, this block will be executed
}
From my own experience, if you got, for example:
Array
(
[1] => Array
(
[0] => a
[1] => b
)
[2] => Array
(
[0] => b
)
[4] => Array
(
[0] => c
)
)
On array_search("a", $array[3]) !== FALSE)
it returns TRUE
the same, so to cover all cases, also on null
element, it's better use:
if ( (array_search($element, $array) !== FALSE) && ($array) ) {
echo "found";
}
Hope it helps.
精彩评论