Array Functions

PHP in_array() Function

Topic: PHP Array ReferencePrev|Next

Description

The in_array() function checks if a value exists in an array.

The following table summarizes the technical details of this function.

Return Value: Returns TRUE if searched value is found in the array, or FALSE otherwise.
Version: PHP 4+

Syntax

The basic syntax of the in_array() function is given with:

in_array(search, array, strict);

The following example shows the in_array() function in action.

<?php
// Sample array
$colors = array("red", "green", "blue", "orange", "yellow");

// Searching value inside colors array
if(in_array("orange", $colors)){
    echo "Match found!";
} else{
    echo "No match found!";
}
?>

Parameters

The in_array() function accepts the following parameters.

Parameter Description
search Required. Specifies the searched value. If it is a string, the comparison is done in a case-sensitive manner.
array Required. Specifies the array to be searched.
strict Optional. Determines if strict comparison (===) should be used during the value search. Possible values are true and false. Default value is false.

More Examples

Here're some more examples showing how in_array() function actually works:

The following example will also match the type of searched value using strict parameter.

<?php
// Sample array
$numbers = array(5, 7, "10", 12, 15, "18", 20);

// Searching value inside numbers array
if(in_array("15", $numbers, true)){
    echo "Match found!";
} else{
    echo "No match found!";
}
?>

You can also pass an array as search parameter as shown in the following example:

<?php
// Sample array
$mixed = array(array("a", "b"), array("x", "y"), "z");

// Searching value inside mixed array
if(in_array(array("x", "y"), $mixed)){
    echo "Match found!";
} else{
    echo "No match found!";
}
?>
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties