Heim  >  Artikel  >  Backend-Entwicklung  >  php数组函数序列之in_array() - 查找数组中是否存在指定值_PHP教程

php数组函数序列之in_array() - 查找数组中是否存在指定值_PHP教程

WBOY
WBOYOriginal
2016-07-21 15:23:54887Durchsuche

in_array()定义和用法

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

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

说明
如果给定的值 value 存在于数组 array 中则返回 true。如果第三个参数设置为 true,函数只有在元素存在于数组中且数据类型与给定值相同时才返回 true。

如果没有在数组中找到参数,函数返回 false。

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

例子 1

复制代码 代码如下:

$people = array("Peter", "Joe", "Glenn", "Cleveland");

if (in_array("Glenn",$people))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?>

输出:

Match found例子 2
复制代码 代码如下:

$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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/324423.htmlTechArticlein_array()定义和用法 in_array() 函数查找数组中是否存在指定值。 语法 in_array(value,array,type)参数 描述 value 必需。规定要在数组搜索的值。 a...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn