Menu Close

php in_array check for multiple values

codeamend
It depend on how many values to check using in_array. If more than two, better way is loops through array and runs an in_array of it on the checking array. Returns true once something is found, else it returns false.

Examples:

<?php
$main_array = array(1,2,3,4,5);
$check_value = array(1,3,6,8);

foreach ($check_value as $value) {
    if (in_array($value, $main_array)) {
        true;
    } else {
        return false;
    }
}
?> 
<?php
if(in_array('foo',$arg) && in_array('bar',$arg)){
    //both of them are in $arg
}
 
if(in_array('foo',$arg) || in_array('bar',$arg)){
    //at least one of them are in $arg
}
?> 

Share

Posted in PHP

You can also read...

Leave a Reply

Your email address will not be published.