php in_array() 检查数组中是否存在某个值
in_array检查数组中是否存在某个值
基本语法:
bool in_array(mixed $needle,array $haystack,bool $strict=false)
在 haystack 中搜索 needle
参数介绍
| needle | 必需。规定要在数组搜索的值。如果是字符串,则比较是区分大小写的。 |
| haystack | 必需。规定要搜索的数组。 |
| strict | 可选。如果设置该参数为 true,则 in_array() 函数还会检查 needle 的类型是否和 haystack 中的相同。 |
返回值
如果找到 needle 则返回 true ,否则返回 false 。
实例:
<?php
$os = array(
"mac",
"nt",
"irix",
"linux"
);
if (in_array("irix", $os)) {
echo "got irix";
}
if (in_array("mac", $os)) {
echo "got mac";
}
?>
在线运行第二个条件失败,因为 in_array() 是区分大小写的,所以以上程序显示为:
got irix
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!