PHP in_array() 函數
實例
在數組中搜索值"Glenn" ,並輸出一些文本:
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn", $people))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?>
$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn", $people))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?>
運行實例»
定義和用法
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>";
}
?>
$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>";
}
?>
運行實例»
完整的PHP Array參考手冊