PHP数组案例声明检查

时间:2013-08-30 00:19:51

标签: php

我有一个变量,可以是组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;
    }
}

1 个答案:

答案 0 :(得分:2)

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

if (!is_array($groups)) {
    $groups = array($groups);
}
// Now use the array code