PHP in_array() 函数

定义和用法

in_array() 函数搜索数组中是否存在指定的值。

注释:如果 search 参数是字符串且 type 参数被设置为 TRUE,则搜索区分大小写。

语法

in_array(search,array,type)
参数 描述
search 必需。规定要在数组搜索的值。
array 必需。规定要搜索的数组。
type 可选。如果该参数设置为 TRUE,则 in_array() 函数检查搜索的数据与数组的值的类型是否相同。

技术细节

返回值: 如果在数组中找到值则返回 TRUE,否则返回 FALSE。
PHP 版本: 4+
更新日志 自 PHP 4.2 起,search 参数可以是一个数组。

更多实例

实例 1

 <?php
$people = array("Peter", "Joe", "Glenn", "Cleveland", 23);

if (in_array("23", $people, TRUE))
{
echo "Match found<br>";
}
else
{
echo "Match not found<br>";
}
if (in_array("Glenn",$people, TRUE))
{
echo "Match found<br>";
}
else
{
echo "Match not found<br>";
}

if (in_array(23,$people, TRUE))
{
echo "Match found<br>";
}
else
{
echo "Match not found<br>";
}
?>