php array_search 为第一个元素返回 0? [英] php array_search returning 0 for the first element?

查看:30
本文介绍了php array_search 为第一个元素返回 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

$restaurant = array('/restaurant_pos/', '/bar_nightclub_pos/')
$current_page = $_SERVER['REQUEST_URI'];

if (array_search($current_page, $restaurant)) {
    echo "KEEP ME";
}

问题是 array_search 返回 0,因为 '/restaurant_pos/' 是数组中的第一个元素,它导致 if 失败......关于如何检查值是否在数组中而不失败的任何想法第一个元素

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";
}

手册链接:http://php.net/manual/en/function.array-search.php

这篇关于php array_search 为第一个元素返回 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆