php array contains value


 

php array contains value

This PHP Tutorial section we will demonstrates the use of in_array() function in the php arrays

This PHP Tutorial section we will demonstrates the use of in_array() function in the php arrays

  • PHP in_array() function is used to check whether the given value is presesnt in the array or not.


PHP Array Contains Value Example

in_array() function is used to check whether the given value is presesnt in the  array or not.

Example

<?php

    $ar1=array("jack","mac","rob","tom","jennifer");

    $val="mac";

    $val1="mic";

    foreach ($ar1 as $a)

    echo " ".$a;

    echo "<br>";

    if(in_array($val,$ar1)) {

    echo "$val is present in the array";

       }

    else {

    echo "$val is not present in the array";

       }

    echo "<br>";

    if(in_array($val1,$ar1)) {

    echo "$val1 is present in the array";

       }

    else {

    echo "$val1 is not present in the array";

       }

?>

    Output

    jack mac rob tom jennifer
    mac is present in the array
    mic is not present in the array

Ads