如果其中一个或两个条件为真

时间:2015-02-27 23:47:53

标签: php arrays foreach

如果其中一个或两个条件都返回true,我试图获取数组值,例如:

    $emailarr = array('arsalan@gmail.com', 'aquarious@yahoo.com', 'imran@google.com');    
foreach ($emailarr as $email) {
   if ($email !== 'aquarious@yahoo.com' || 'imran@google.com'){
    echo 'Email Send to '. $email   ;
   }   
}

实际上我需要的是

if only (aquarious@yahoo.com) return true
If only (imran@google.com)    return true
if both (aquarious@yahoo.com and imran@google.com) return true

提前感谢。

3 个答案:

答案 0 :(得分:1)

只需将您的逻辑更改为:

if ($email == 'aquarious@yahoo.com' || $email ==  'imran@google.com'){
    echo 'Email Send to '. $email   ;
}

输出:

Email Send to aquarious@yahoo.com
Email Send to imran@google.com   

答案 1 :(得分:0)

你不需要“两个”条件,你只需要“两个”。

您也可以使用if (in_array($email, array(email1, email2...)) {...}

(在foreach中,“both”不会在同一次迭代中发生。)

答案 2 :(得分:0)

我不确定你想做什么,但实现目标的一种方法是:

$emails = ['arsalan@gmail.com', 'aquarious@yahoo.com', 'imran@google.com'];

if (count($emails) == 1) {
    if (in_array('aquarious@yahoo.com', $emails)) {
        return true;
    } else if (in_array('imran@google.com', $emails)) {
        return true;
    }
} else if ((count($emails) == 2) && (in_array('aquarious@yahoo.com', $emails)) && (in_array('imran@google.com', $emails))) {
    return true;
}

return false;

此外,将对象类型添加到变量名称不是一种好习惯,就像在" $ email_arrays&#34 ;;中所做的那样。 $ email就好了。

相关问题