php使用in_array() 检查数组中是否存在某个值

2018-06-20 07:24 阅读 1,641 次 评论 0 条

基本语法:

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

版权声明:本文著作权归原作者所有,欢迎分享本文,谢谢支持!
转载请注明:php使用in_array() 检查数组中是否存在某个值 | 雨晨博客
分类:PHP, 程序笔记 标签:,

发表评论


表情