PHP in_array () function
Examples
Searches the array value "Glenn", and outputs some text:
<?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";
}
?>
Running instance »
Definition and Usage
in_array () function to specify the value of the existence of the search array.
Note: If thesearch parameter is a string and the type parameter is set to TRUE, the search is case sensitive.
grammar
in_array( search,array,type )
参数 | 描述 |
---|---|
search | 必需。规定要在数组搜索的值。 |
array | 必需。规定要搜索的数组。 |
type | 可选。如果该参数设置为 TRUE,则 in_array() 函数检查搜索的数据与数组的值的类型是否相同。 |
technical details
return value: | If you find the value in the array it returns TRUE, otherwise returns FALSE. |
---|---|
PHP version: | 4+ |
Update Log | Since PHP 4.2 onwards, search parameter can be an array. |
More examples
Example 1
Use all parameters:
<?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>";
}
?>
Running instance »
Complete PHP Array Reference