有限的foreach?

Limited Foreach?

我正在寻找一种只在 foreach 循环中放置几个​​变量的方法。

基本上,我正在寻找空值,并使用 die 告诉用户返回并填写表单。

这是我的数组:

$posted = array(
$sales_rep = strtoupper($_POST['sales_rep']),
$c_first_name = strtoupper($_POST['c_first_name']),
$c_last_name = strtoupper($_POST['c_last_name']),
$c_address = strtoupper($_POST['c_address']),
$c_zip = strtoupper($_POST['c_zip']),
$deal_number = strtoupper($_POST['deal_number']),
$deal_stock_number = strtoupper($_POST['deal_stock_number']),
$deal_selling_model = strtoupper($_POST['deal_selling_model']),
$deal_trade_in_model_1 = strtoupper($_POST['deal_trade_in_model_1']),
$deal_trade_in_amount_1 = strtoupper($_POST['deal_trade_in_amount_1']),
$deal_trade_in_model_2 = strtoupper($_POST['deal_trade_in_model_2']),
$deal_trade_in_amount_2 = strtoupper($_POST['deal_trade_in_amount_2']),
$deal_new_status = strtoupper($_POST['deal_new_status']),
$deal_dh = strtoupper($_POST['deal_dh']),
$deal_finance_amount = strtoupper($_POST['deal_finance_amount']),
$deal_pack = strtoupper($_POST['deal_pack']),
$deal_retro = strtoupper($_POST['deal_retro']),
$deal_holdback = strtoupper($_POST['deal_holdback']),
$deal_reserve_amount = strtoupper($_POST['deal_reserve_amount']),
$deal_warrantee = strtoupper($_POST['deal_warrantee']),
$deal_gap = strtoupper($_POST['deal_gap']),
$deal_etch = strtoupper($_POST['deal_etch']),
$deal_maintenence = strtoupper($_POST['deal_maintenence']),
$deal_gross = strtoupper($_POST['deal_gross'])
);

然而,最后几个不能进入,因为它们在表单中隐藏,除非用户这么说(它们可以为空):

$deal_reserve_amount = strtoupper($_POST['deal_reserve_amount']),
$deal_warrantee = strtoupper($_POST['deal_warrantee']),
$deal_gap = strtoupper($_POST['deal_gap']),
$deal_etch = strtoupper($_POST['deal_etch']),
$deal_maintenence = strtoupper($_POST['deal_maintenence']),
$deal_gross = strtoupper($_POST['deal_gross'])

我知道可能有一种方法可以用 if 做到这一点,但我试图避免让我的代码混乱,我正在寻找一种非常巧妙的方法来做到这一点,也许在 foreach 中本身。

我的问题:是否可以说在 foreach 语句中只放入几个变量? (限制我在数组末尾预留的那些)

如果这不起作用,我可能会 运行 一个 Javascript 函数来填充这些值,以实际上按照 "NULL," 的方式说出一些东西,但令人讨厌的是,这不是最有效的方法来做到这一点。

您可以创建一个隐藏键数组:

$hidden = array('deal_maintenence', 'deal_gross');

然后您可以遍历 $_POST 数组并检查密钥是否为隐藏密钥:

foreach ($_POST as $key => $value) {
    if (!in_array($key, $hidden)) {
        if (!empty($_POST[$key])) {
            // valid
        } else {
            // invalid
        }
    }
}

或者您可以通过指定要验证的密钥以其他方式进行:

$keys = array('sales_rep', 'c_first_name', 'c_last_name');

foreach ($keys as $key) {
    if (isset($_POST[$key]) && !empty($_POST[$key])) {
        // valid
    } else {
        // invalid
    }
}

也许你可以尝试这样的事情:

$variables = array ("c_first_name", "c_last_name" /* etc. */);
foreach ($variables as $v)
{
    if (isset ($_POST[$v]))
    {
        $posted[] = $$v = $_POST[$v];
    }
    else
    {
        // scold the user for not having set the variable
    }
}

话虽如此,我宁愿将表单封装在 PHP class 中以避免创建一堆变量。

我想出了一个非常简单的方法...

$v_info = array(  // V being visible
$sales_rep = strtoupper($_POST['sales_rep']),
$c_first_name = strtoupper($_POST['c_first_name']),
$c_last_name = strtoupper($_POST['c_last_name']),
$c_address = strtoupper($_POST['c_address']),
$c_zip = strtoupper($_POST['c_zip']),
$deal_number = strtoupper($_POST['deal_number']),
$deal_stock_number = strtoupper($_POST['deal_stock_number']),
$deal_selling_model = strtoupper($_POST['deal_selling_model']),
$deal_trade_in_model_1 = strtoupper($_POST['deal_trade_in_model_1']),
$deal_trade_in_amount_1 = strtoupper($_POST['deal_trade_in_amount_1']),
$deal_trade_in_model_2 = strtoupper($_POST['deal_trade_in_model_2']),
$deal_trade_in_amount_2 = strtoupper($_POST['deal_trade_in_amount_2']),
$deal_new_status = strtoupper($_POST['deal_new_status']),
$deal_dh = strtoupper($_POST['deal_dh']),
$deal_finance_amount = strtoupper($_POST['deal_finance_amount']),
$deal_pack = strtoupper($_POST['deal_pack']),
$deal_retro = strtoupper($_POST['deal_retro']),
$deal_holdback = strtoupper($_POST['deal_holdback'])
);
$nv_info = array( // NV being not visible
$deal_reserve_amount = strtoupper($_POST['deal_reserve_amount']),
$deal_warrantee = strtoupper($_POST['deal_warrantee']),
$deal_gap = strtoupper($_POST['deal_gap']),
$deal_etch = strtoupper($_POST['deal_etch']),
$deal_maintenence = strtoupper($_POST['deal_maintenence']),
$deal_gross = strtoupper($_POST['deal_gross'])
);
foreach ($v_info as $raw) {
if ($raw == null) {
    die("Please fill in all the required elements in the form. ");
}
}
$merged_arrays = array_merge($v_info, $nv_info);

这最有效,因为我计划将它放入数据库中,为了保持一致性,我希望它全部来自一个数组。