0

我有一个变量,它可能是组 id 的字符串或许多组 id 的数组。有没有办法缩短检查以查看它们是否属于一个组。

if(is_array($groups)){
    /* Check for multiple groups */
    $total = count($groups);
    $hasperm = false;
    while($total > 0){
        /* If account has a bad status dont login */
        switch($group[$total]){
            case 4:
                $errmsg_arr[] = array('type'=>'crucial','alert'=>'Alert!','msg'=>'Your account is not active, contact admin');
                failedAttempt($errmsg_arr);
                break;
            case 6:
                $errmsg_arr[] = array('type'=>'crucial','alert'=>'Alert!','msg'=>'Your account is banned, contact admin');
                failedAttempt($errmsg_arr);
                break;
            case 5:
                $errmsg_arr[] = array('type'=>'crucial','alert'=>'Alert!','msg'=>'Your account is scheduled for deletion, if you are reading this you may still have time to recover your account, Call 716-698-9236. $50 Reactivation fee required.');
                failedAttempt($errmsg_arr);
                break;
        }
        $total--;
    }
}else{
    /* If account has a bad status dont login */
    switch($groups){
        case 4:
            $errmsg_arr[] = array('type'=>'crucial','alert'=>'Alert!','msg'=>'Your account is not active, contact admin');
            failedAttempt($errmsg_arr);
            break;
        case 6:
            $errmsg_arr[] = array('type'=>'crucial','alert'=>'Alert!','msg'=>'Your account is banned, contact admin');
            failedAttempt($errmsg_arr);
            break;
        case 5:
            $errmsg_arr[] = array('type'=>'crucial','alert'=>'Alert!','msg'=>'Your account is scheduled for deletion, if you are reading this you may still have time to recover your account, Call 716-698-9236. $50 Reactivation fee required.');
            failedAttempt($errmsg_arr);
            break;
    }
}
4

1 回答 1

2

如果它不是一个数组,则创建一个包含该值的单元素数组。然后使用您已经为数组编写的代码。

if (!is_array($groups)) {
    $groups = array($groups);
}
// Now use the array code
于 2013-08-30T00:23:09.090 回答