发布后更改数组键

时间:2017-08-07 00:42:29

标签: php

我正在为餐饮服务商订购。

form.html

包含许多输入字段,每个字段用于食品。在这个阶段,它们通常被命名。例如:

<td><input name="Fish1" class="val2"/></td>
<td><input name="Fish2" class="val2"/></td>
<td><input name="Fish3" class="val2"/></td>
<td><input name="BakedSalmon1" class="val2"/></td>
<td><input name="BakedSalmon2" class="val2"/></td>
<td><input name="BakedSalmon3" class="val2"/></td>
<td><input name="RawSalmon1" class="val2"/></td>
<td><input name="RawSalmon2" class="val2"/></td>
<td><input name="RawSalmon3" class="val2"/></td>

它还在表单的开头有这些(和更多)隐藏字段:

<input type="hidden" name="OrderID" value="<? echo time(); ?>">
<input type="hidden" name="OrderDate" value="<? echo $today; ?>">
<input type="hidden" name="Name" value="">

按下&#34;查看您的订单&#34;后,表单会被发送到review.php

review.php

在这个阶段,我想拥有这些项目的具体名称。所以,例如:

/* Create Clean Array */
$CleanPost = $_POST;
foreach($CleanPost as $key => $link) 
{ 
    if($link === '') 
    { 
        unset($CleanPost[$key]); 
    } 
} 

/* Rename Array Keys */
foreach ($CleanPost as $keys => $var) {
if (empty($vars)) continue; #skip if vars is empty
if ($keys == "Fish1") { $keys = "Sweet n’ Sour Salmon";}
if ($keys == "Fish2") { $keys = "Baked Fish";}
if ($keys == "Fish3") { $keys = "Fish in Vegetables and Tomato Sauce";}
if ($keys == "BakedSalmon1") { $keys = "Teriyaki Salmon - Baked";}
if ($keys == "BakedSalmon2") { $keys = "Glazed Salmon -  Baked";}
if ($keys == "BakedSalmon3") { $keys = "Seasoned Salmon - Baked";}
if ($keys == "RawSalmon1") { $keys = "Teriyaki Salmon - Raw";}
if ($keys == "RawSalmon2") { $keys = "Glazed Salmon -  Raw";}
if ($keys == "RawSalmon3") { $keys = "Seasoned Salmon - Raw";}

我尝试这样做的一个原因是因为我试图向客户展示他们订单的预览,显然一般的Fish1 Fish2并不会有所帮助。此外,还有一封确认电子邮件/发票&#34;将被发送给客户和餐饮服务商,这将需要显示具体的项目名称。

以下是我用来显示订单评论的代码:

<p>Please review your order:</p>
<form action="submit.php" method="post" id="orderform" name="orderform">
    <table>
        <?
        foreach ($CleanPost as $index => $val) {
            echo("<tr><td>$index</td><td>$val</td></tr>");
        }
        ?>
    </table>

我已经尝试array_combine,但未能成功完成工作。老实说,我在过去的5个小时里尝试过很多东西,并且没有成功。我知道这听起来很陈词滥调,但我很认真:)

任何提示?

提前致谢。

P.S。我有一个所有项目全名的数组,以防万一。

修改: 我从form.html中删除了隐藏的输入字段。然后我尝试了:

$PostVals = array();
foreach ($_POST as $key => $var) {
    $PostVals[] = $var;
}
$NewKeysVals = array();
$NewKeysVals = array_combine($FullNames, $PostVals);

--
foreach ($NewKeysVals as $index => $val) {
echo("<tr><td>$index</td><td>$val</td></tr>");

它有效!非常非常奇怪。

今天凌晨4点,我想我已经花了太长时间看这个,以至于我浪费了其他所有人的时间。明天会给它一个新的面貌,并在必要时回复。谢谢大家(特别是@Spectarion)如此迅速地回答并愿意提供帮助!

1 个答案:

答案 0 :(得分:1)

我建议使用一系列密钥及其真实姓名。

// You posted only portion of items. Please, enter all of them.
$keyNames = [
    "Fish1" => "Sweet n’ Sour Salmon",
    "Fish2" => "Baked Fish",
    "Fish3" => "Fish in Vegetables and Tomato Sauce",
    "BakedSalmon1" => "Teriyaki Salmon - Baked",
    "BakedSalmon2" => "Glazed Salmon -  Baked",
    "BakedSalmon3" => "Seasoned Salmon - Baked",
    "RawSalmon1" => "Teriyaki Salmon - Raw",
    "RawSalmon2" => "Glazed Salmon -  Raw",
    "RawSalmon3" => "Seasoned Salmon - Raw"
    // etc ...
];

foreach ($CleanPost as $keys => $var)
{
    // If key name exists, assign it to the current $keys variable.
    if(isset($var) && isset($keyNames[$keys])) {
        $keys = $keyNames[$keys];
    }
}