Easy Tutorial
❮ Func Array Intersect Func Array Combine ❯

PHP in_array() Function

Complete PHP Array Reference Manual

Example

Search for the value "tutorialpro" in an array and output some text:

<?php
$sites = array("Google", "tutorialpro", "Taobao", "Facebook");

if (in_array("tutorialpro", $sites))
{
    echo "Match found!";
}
else
{
    echo "No match found!";
}
?>

Definition and Usage

The in_array() function searches an array for a specific value.


Syntax

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Parameter Description
needle Required. Specifies the value to search for.
haystack Required. Specifies the array to search in.
strict Optional. If this parameter is set to TRUE, the in_array() function checks both the value and the type of the data.

Technical Details

Return Value: Returns TRUE if the value is found in the array, otherwise returns FALSE.
PHP Version: 4+
--- ---
Changelog Since PHP 4.2, the search parameter can be an array.
--- ---

More Examples

Example 1

Using 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>";
}
?>

❮ Func Array Intersect Func Array Combine ❯