How To Use In Array Function With Example Using PHP

admin_img Posted By Bajarangi soft , Posted On 21-09-2020

The in_array() function is used to check whether a given value exists in an array or not.in_array() function is an inbuilt function in PHP.

PHP in_array() function with example

Syntax for in_array() Function in PHP
 

in_array(search, array, type)


Parameter   Description

search  Specifies the which element should to search for
array  Specifies the input array to search
type   If this parameter is set to TRUE, the in_array() function searches for the search-string and specific type in the array.
 

Example(1)

<?php
$BIKE = array("Bike1", "Bike2", "Bike3", "Bike4");

if (in_array("Bike1", $BIKE))
    echo "element found in an array";
else
    echo "element not found in an array";
?>

above example we have one array variable which content array elements so by using in_array() function we can search which element need to be searched in array .

Complete code of in_array() function.
 

<!DOCTYPE html>
<html>
<head>
    <title>How to use PHP in_array() function with example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>

<body>
<div class="container">
    <br>
    <br>
    <br>
    <div class="text-center">
        <h1>PHP in_array() function</h1>
    </div>
    <br>
    <br>
    <h2></h2>
    <div class="well">
        <?php
        $BIKE = array("Bike1", "Bike2", "Bike3", "Bike4");

        if (in_array("Bike1", $BIKE))
        {
            echo "<h1>Element found in an array</h1>";
        }
        else
        {
            echo "<h1>Element not found in an array</h1>";
        }
        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post