PHP 中的 in_array() 函数

phpprogrammingserver side programming更新于 2025/7/9 3:52:17

in_array() 函数检查数组中是否存在指定值。

语法

in_array(find, arr, type)

参数

  • find − 待搜索的值

  • arr − 待搜索的数组

  • type − 执行搜索的模式。如果参数设置为 TRUE,则它会在数组中查找搜索字符串和特定类型。

返回

如果在数组中找到该值,in_array() 函数返回 TRUE,否则返回 FALSE。

示例

下面是一个例子 −

<?php
$stu = array("Tom", "Amit", "Peter", "Rahul");
if (in_array("Amit", $stu)) {
   echo "Match!";
} else {
   echo "No Match!";
}
?>

输出

以下是输出 −

Match!

示例

让我们看另一个例子 −

<?php
$pro = array("Tom", "Amit", "Peter", "Rahul", 5, 10);
if (in_array(5, $pro, TRUE)) {
   echo "Match!";
} else {
   echo "No Match!";
}
?>

输出

以下是输出 −

Match!

相关文章