Home Tutorial Php Phparray php array contains value

 
 

php array contains value
Posted on: October 28, 2009 at 12:00 AM
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

Related Tags for php array contains value:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.