PHP in_array()Functions
Examples
Search for the value "Example" in an array and output some text:
$sites = array("Google", "Example", "Taobao", "Facebook");
if (in_array("Example", $sites))
{
echo "Match found!";
}
else
{
echo "No match found!";
}
Run example »
Definition and Usage
The in_array() function searches an array for a specified value.
Syntax
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
| Parameters | Description |
|---|---|
| needle | Required. Specifies the value to search for in the array. |
| haystack | Required. Specifies the array to search. |
| strict | Optional. If this parameter is set to TRUE, the in_array() function checks whether the searched data and the array value are of the same type. |
Technical Details
| Return value: | Returns TRUE if the value is found in the array, otherwise returns FALSE. |
|---|---|
| PHP Version: | 4+ |
| Changelog | As of PHP 4.2, the search parameter can be an array. |
More Examples
Example 1
Using all parameters:
$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>";
}
Run example »
Complete PHP Array Reference Other Extensions