重构每个


Refactor foreach?

有没有办法提高效率?

    //After trimming validate
    foreach ($_POST as $key => $value) {
        //Check to see if any field is blank
        if ($key == 'ref_type') {
            if (!in_array($key, array('loan', 'member'))) {
                $errors['ref_type'] = 'Choose one of the presented options.';
            }
        } 
        if ($key == 'loan_ref') {
            if ($value == '') {
                $errors['loan_ref'] = 'This field must not be empty.';
            }
        }
        if ($key == 'member_ref') {
            if ($value == '') {
                $errors['member_ref'] = 'This field must not be empty.';
            }
        }
        if ($key == 'transaction_amount') {
            if ($value == '') {
                $errors['transaction_amount'] = 'This field must not be empty.';
            }
        }
        if ($key == 'custom_description') {
            if ($value == '') {
                $errors['custom_description'] = 'This field must not be empty.';
            }
        }
    }

也许代替如果的使用是in_array?

谢谢

也许不是最好的,但仍然更好。

$expected = array('loan_ref', 'member_ref' /* etc. */);
//After trimming validate
foreach ($_POST as $key => $value) {
//Check to see if any field is blank
    if ($key == 'ref_type') {
            if (!in_array($key, array('loan', 'member'))) {
                $errors['ref_type'] = 'Choose one of the presented options.';
            }
    }
    if (in_array($key, $expected) && empty($value)) {
             $errors[$key] = 'This field must not be empty.';
    }
}

根据乔纳森·莱因哈特的评论进行编辑。

一起:

//After trimming validate
foreach ($_POST as $key => $value) {
    //Check to see if any field is blank
    $not_empty_list = array("custom_description", "transaction_amount", "member_ref", "loan_ref");
    if ($value == '' && in_array($value,$not_empty_list)) {
        $errors[$value] = 'This field must not be empty.';
    }
    if ($key == 'ref_type' && !in_array($key, array('loan', 'member'))) {
            $errors['ref_type'] = 'Choose one of the presented options.';
    } 
}

当然,这是假设$_POST可以拥有比"custom_description"、"transaction_amount"、"member_ref"、"loan_ref"中更多的索引。


循序渐进

此条件:

$list=array("custom_description", "transaction_amount", "member_ref", "loan_ref");
if ($value == '' && in_array($value,$list)) {
   $errors[$value] = 'This field must not be empty.';
}

可以替换此块:

    if ($key == 'loan_ref') {
        if ($value == '') {
            $errors['loan_ref'] = 'This field must not be empty.';
        }
    }
    if ($key == 'member_ref') {
        if ($value == '') {
            $errors['member_ref'] = 'This field must not be empty.';
        }
    }
    if ($key == 'transaction_amount') {
        if ($value == '') {
            $errors['transaction_amount'] = 'This field must not be empty.';
        }
    }
    if ($key == 'custom_description') {
        if ($value == '') {
            $errors['custom_description'] = 'This field must not be empty.';
        }
    }

然后在另一部分中,您可以加入这两个条件。从:

    if ($key == 'ref_type') {
        if (!in_array($key, array('loan', 'member'))) {
            $errors['ref_type'] = 'Choose one of the presented options.';
        }
    } 

自:

    if ($key == 'ref_type' && !in_array($key, array('loan', 'member'))) {
            $errors['ref_type'] = 'Choose one of the presented options.';
    } 

首先,你可以使用 if {} else if {},因为键只能等于一个值。

您也可以使用交换机。

为什么要迭代? 只需直接访问您想要的内容:

if (isset($_POST['ref_type')) {
    if (!in_array($_POST['ref_type'], array('loan', 'member'))
        // Error, bad entry given
}
else {
    // Error, ref_type is missing
}

请注意,您的代码不会检查所需的 POST 变量。如果它们不存在,foreach 循环根本不在乎。 这样,您就可以显式检查所需的变量。

我会这样做:
第一个是 execption,这就是为什么我像你一样使用那个。然后,我不是检查键是否与字符串匹配,而是首先检查它是否为空。只有当它为空时,我才会检查更具体,并按键检查。
不过,整个in_array测试很奇怪。您检查密钥是否与某些内容匹配,并继续测试它是否是其他内容......你们都知道不是。

//After trimming validate
foreach ($_POST as $key => $value) {
    //Check to see if any field is blank
    if ($key == 'ref_type') {
        // The following test is weird, you allready know $key=='ref_type'
        if (!in_array($key, array('loan', 'member'))) {
            $errors['ref_type'] = 'Choose one of the presented options.';
        }
    } 
    else{
        if ($value == '') {
                if ($key == 'loan_ref') {          $errors['loan_ref'] = 'This field must not be empty.'; }
            elseif ($key == 'member_ref') {        $errors['member_ref'] = 'This field must not be empty.'; }
            elseif ($key == 'transaction_amount'){ $errors['transaction_amount'] = 'This field must not be empty.'; }
            elseif ($key == 'custom_description') {    $errors['custom_description'] = 'This field must not be empty.'; }
        }
    }
}
function checkRef($key) {
   switch ($key) {
            case 'ref_type':
                if (!in_array($key, array('loan', 'member'))) {
                $output = 'Choose one of the presented options.';
            }
                break;
            case 2:
                //repeat for other status
                break;
            case 3:
                $output = "Silver";
                break;
            case 4:
                $output = "Bronze";
                break;
            default:
                $output = "ROWS EMPTY";
        }
    return $output;
}
foreach ($_POST as $key => $value) {
   $errors[$key] = checkRef($key);
}

使用函数的类似内容

您可以使用switch/case:并将类似的情况分组:

    foreach ($_POST as $key => $value) {
        //Check to see if any field is blank
        switch($key){
         case 'ref_type':
            if (!in_array($key, array('loan', 'member'))) {
                $errors['ref_type'] = 'Choose one of the presented options.';
            }
            break;
         case 'loan_ref':
         case 'member_ref':
         case 'transaction_amount':
         case 'custom_description':
            if ($value == '') {
                $errors[$key] = 'This field must not be empty.';
            }
            break;
        default:
            // no check for others fields ?
            break;
        }
     }

我个人也会用default:替换 4 个类似的案例

ref_type的处理是不同的,所以你可以把它分开。 然后循环访问其余字段并检查它们:

if(!isset($_POST['ref_type']) ||
   !in_array($_POST['ref_type'], array('loan', 'member')) {
    $errors['ref_type'] = 'Choose one of the presented options.';
}
$fields = array('loan_ref', 'member_ref', 'transaction_amount', 'custom_description');
foreach($fields as $key) {
    if(!isset($_POST[$key]) || $_POST[$key] == '') {
        $errors[$key] = "Field $key must not be empty.";
    }
}