Share on Google+Share on Google+

php array contains value

Advertisement
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

Advertisements

Advertisement

Posted on: October 28, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.