重构foreach?

时间:2013-11-15 15:46:00

标签: php 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.';
            }
        }
    }

也许而不是if的使用是in_array?

由于

8 个答案:

答案 0 :(得分:3)

也许不是最好的,但还是更好。

$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.';
    }
}

根据Jonathon Reinhart的评论编辑。

答案 1 :(得分:3)

一起:

//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.';
    } 

答案 2 :(得分:1)

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

您还可以使用Switch

答案 3 :(得分:1)

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

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循环根本就不在乎。这样,您就可以明确检查所需的变量。

答案 4 :(得分:1)

我会这样做:
第一个是一个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.'; }
        }
    }
}

答案 5 :(得分:1)

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);
}

使用函数

这样的东西

答案 6 :(得分:1)

您可以使用开关/案例:并对类似案例进行分组:

    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个类似的案例

答案 7 :(得分:1)

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.";
    }
}