php

Check if item exist in array using PHP

<?php
    $arr = ["car", "bike", "plane"];
    $item = "plane";

    if ( in_array($item, $arr) ) {
        echo "Item exist in the given array";
    } else {
        echo "Item does not exist in the array";
    }
?>

To check whether an item exist in an array or not using PHP, you can use in_array() inbuild PHP function.

in_array($item, $array, $mode)

in_array() function takes two required parameters as $item and $array and one optional parameter $mode. The description is as below:

$item(required): The first parameter is the item or value that you want to check against array. This is a required parameter.

$array(required): The second required parameter is $array which is the array that contains the items or values in it.

$mode(optional): This is an optional parameter is a False by default. If you pass True to it, the function will check the exact value with its type also.

Was this helpful?