in_array() 函数的定义与基本用途
in_array() 函数的定义与基本用途
该函数主要用于检测某个特定值是否存在于指定的数组结构中。
语法结构
in_array(value, array, type)
参数说明
value:必填参数,指定需要在数组中查找的目标值。
array:必填参数,指定被搜索的目标数组。
type:可选参数。若将其设置为 true,函数将同时检查搜索数据的类型是否与数组中的值类型完全相同。
功能说明
当给定的 value 存在于 array 中时,函数返回 true。如果第三个参数 type 被设为 true,则只有当元素存在且其数据类型与给定值相同时,才返回 true。
若在数组中未找到指定的参数,函数将返回 false。
注意:当 value 为字符串且 type 设为 true 时,搜索过程区分大小写。
示例 1
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn",$people))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?> 输出结果:
Match found
示例 2
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland", 23);
if (in_array("23",$people, TRUE))
{
echo "Match found"
";
}
else
{
echo "Match not found"
";
} if (in_array("Glenn",$people, TRUE))
{
echo "Match found"
";
}
else
{
echo "Match not found"
";
} if (in_array(23,$people, TRUE))
{
echo "Match found"
";
}
else
{
echo "Match not found"
";
}
?> 输出结果:
Match not found
Match found
Match found