简体   繁体   中英

Checks if several values exists in an array

I want check if exists several values (EX: 5 , 110 ) in array return is true. (without use loop)

I don't use from loop because i want load a page if return was true

AS:

if(in_array('5' OR '110', array('5,4,2,66,12,110'))){
    echo 'true';//Load page
}else{
    echo 'false';
}

How is it?

if(array_intersect(array('15', '110'), explode(',', '5,4,2,66,12,110'))){
    echo 'true';//Load a page
}else{
    echo 'false';
}
$a=array(5,4,2,66,12,110);
if(in_array('5',$a) OR in_array('10',$a )){
    echo 'true';//Load page
}else{
    echo 'false';
}

If you are looking for a fast method use this:

if ( preg_match ( '/(^|,)(5|110)(,|$)/','5,4,2,66,12,110' ) )
  return true;
else
  return false;

EDIT

if ( preg_match ( "/(^|,)(" . implode ('|', $needles ) . ")(,|$)/", implode (',', $haystack ) ) )
  return true;
else
  return false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM