在数组中搜索值 "Glenn" 并输出一些文本:
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn", $people))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?>
亲自试一试 »
in_array() 函数在数组中搜索特定值。
笔记:如果搜索参数是字符串并且类型参数设置为 TRUE,则搜索区分大小写。
in_array(
search, array, type)
Parameter | Description |
---|---|
search | Required. Specifies the what to search for |
array | Required. Specifies the array to search |
type | Optional. If this parameter is set to TRUE, the in_array() function searches for the search-string and specific type in the array. |
返回值: | 如果在数组中找到该值,则返回 TRUE,否则返回 FALSE |
---|---|
PHP 版本: | 4+ |
PHP 变更日志: | PHP 4.2:搜索参数现在可以是一个数组 |
使用所有参数:
<?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>";
}
?>
亲自试一试 »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!