仅获取指定名称的电子邮件

Get only a specified name of email

我对 preg_math 有疑问。所以我有这个方法:

public function filterUsers($a_users){

    $a_authorizedEmail = array(
        '@test.com',
        '@test1.com'
    );

    $a_response = array();
    foreach ($a_users as $user) {
        if(false !== $email_user = EmailRepository::getEmail($user['id'])){
            foreach($a_authorizedEmail as $email){
                echo $email_user;
                if(preg_match($email, $email_user)){
                    $a_response[]= $user;
                }
            }
        }
    }
    return $a_response;
}

$a_users 的数组:我有一个用户的电子邮件 hcost@test.com。但是在 return 中数组是空的。可能我没有进行正确的验证。请帮助我

这样做:

public function filterUsers($a_users){

    $a_authorizedEmail = array(
        '@test.com',
        '@test1.com'
    );
    $pattern = "/".implode("|",$a_authorizedEmail)."$/"; //Note the "/" start and end delimiter as well as the $ that indicates end of line
    $a_response = array();
    foreach ($a_users as $user) {
        $email_user = EmailRepository::getEmail($user['id']);  
        if(false !== $email_user){ 
            echo $email_user;
            if(preg_match($pattern, $email_user)){
                $a_response[]= $user;
            }
        }
    }
    return $a_response;
}