Online Tutorials & Training Materials | STechies.com
Register Login

in_array in PHP

|| 0

in_array in PHP
Stechies

Definition

PHP in_array() is an inbuilt PHP function which is used to check if a specific value exists in an Array or not. 

Syntax

in_array ($value, $array, $type)

Parameters

Parameter Type Description

$value  (mandatory)

Integer,string, float,boolean etc

It is the specific value to be searched in array

Note: If this parameter is array type then the search performed will be case sensitive

$array (mandatory) array It is the array name in which search will be performed
$type (optional) boolean

If this parameter is set TRUE than the in_array() function searches for the same type of value specified in the '$value' parameter.

The default value of this parameter is false

Return Value

PHP in_array() function returns TRUE if the $value is found in the array and FALSE if the $value is not found in the array.

Example of in_array Function in PHP

Example 1

<?
#PHP program to illustrate the working of in_array Function in PHP

#Simple program - 1

//Array variable of Cars Brands
$carBrands = array('Maruti','Honda','Hyundai','Tata','Ford');

//Variable to store the return result of in_array function
$marutiCar = in_array('Maruti', $carBrands);

//Checking the return result is true or false.
/* NOTE: in_array returns the 0 (For False) or 1 (For True)*/

if($marutiCar == 1){
    echo "Brand Maruti is available";
}else{
    echo "Brand Maruti not found";
}
?>

OUTPUT:

Brand Maruti is available

Example 2

<?
#PHP program to illustrate the working of in_array Function of PHP

#Simple program - 2

//Array variable of Cars Brands
$carBrands = array('Maruti','Honda','Hyundai','Tata','Ford');
//Conditional loop with in_array function

/*
    In this Conditional Loop we checking , Is returned
    Result is 1 or not
*/
if(in_array("Honda", $carBrands)){
    echo "Brand Found";
}else{
    echo "Brand not Found";
}

?>

OUTPUT

Brand Found

Example 3: Strict Mode

<?
#PHP program to illustrate the working of in_array Function of PHP

#Advance Example / Strict Mode

//Array of prices
$carPriceInLakhs = array(2.5,4.5,3.0,3,24,56,29,1,0.7,"34");

#Conditional Statement
/*
    First Statement :
    {
        In this Statement we are checking, Is 2.4 is available in our array
        and Boolean TRUE is checking, Is 2.4 needle has the same data type in
        the array or not.
        If Yes, It will run the block for this statement
    }

    Second Statement :
    {
        In this Statement we are checking, Is 24 is available in our array
        and Boolean TRUE is checking, Is 24 needle has the same data type in
        the array or not.
        As we can see 24 is available in the array but it the different data-
        type so, It will not run the block for this statement.
    }

*/

if(in_array(2.5,$carPriceInLakhs,TRUE)){
    echo "Cars available in 2.5 Lakhsn";
}else if(in_array("24",$carPriceInLakhs,True)){
    echo "Cars not available in 24 Lakhsn";
}else{
    echo "Cars not available in this pricen";
}
?>

OUTPUT

Cars available in 2.5 Lakhs