php in_array check for multiple values

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.

$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)) {
             return true;
        }
	else{
	     return false;
	}
    }

If only two, probably can use if:

	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
	}

Reference: http://stackoverflow.com/questions/7542694/in-array-multiple-values

By Keenlio, February 19, 2014

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *


eight − = 5

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>