php in_array()或array_search()无法正常工作 [英] php in_array() or array_search() not working
问题描述
我正在使用一个简单的php脚本在数组之类的元素中寻找
I am using a simple php script to look for an element in an array like
$restricted = array('root/base', 'root2' );
print_r($restricted);
if( array_search('root/base', $restricted) ){
echo "1";
} else {
echo "0";
}
但是我总是得到以下输出
But I am always getting the following output
Array ( [0] => root/base [1] => root2 ) 0
这意味着array_search在给定数组中找不到元素.任何人都可以对发生的事情有所了解吗?
This means that the array_search is failing to find the element in the given array. Can anybody show some light on whats happening?
我也尝试用in_array()替换array_search().但这也返回了相同的错误.
I tried to replace array_search() with in_array() also. But that too returned the same error.
推荐答案
来自PHP DOC
array_search —在数组中搜索给定值,如果成功,则返回相应的键
array_search — Searches the array for a given value and returns the corresponding key if successful
索引为0
,这就是您认为索引失败的原因
The index is 0
that is why you think its fails
使用
array_search('root/base', $restricted) !== false
这篇关于php in_array()或array_search()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!