【发布时间】:2017-09-26 12:09:12
【问题描述】:
我有一个使用 PHP 设置页面输入字段值的表单,例如:
// create and fill inputVarsArr with keys that have empty vals
$troubleInsertInputVarsArr = array_fill_keys(array('status', 'discoverDate', 'resolveDate', 'ticketIssued', 'eqptSystem', 'alarmCode', 'troubleDescription', 'troubleshootingSteps', 'resolveActivities', 'notes'), '');
if (isset ($_POST['insert'])) { // update DB site details
foreach ($troubleInsertInputVarsArr as $key => $val) {
if ($key !== 'discoverDate' && $key !== 'resolveDate') { //deal with dates separately below
$val = $_SESSION[$key] = sanitizeText($_POST[$key]);
echo '<br>(' . __LINE__ . ') ' . $key . ': ' . $val . ' ';
} // close IF ; line 47 echo
} // close FOREACH
foreach ($troubleInsertInputVarsArr as $key => $val) {
echo '<br>(' . __LINE__ . ') ' . $key . ': ' . $val . ' ';
} // close FOREACH // line 52 echo
}
在if 条件下嵌套在foreach 循环中的输入变量回显(第47 行的回显)按我的预期打印出来(输入到表单页面的输入字段中的值):
(47) status: Outstanding
(47) ticketIssued: no
(47) eqptSystem: Tower Lights
(47) alarmCode: Visual
(47) troubleDescription: - no description yet -
(47) troubleshootingSteps: - no steps yet taken -
(47) resolveActivities: - no activities yet decided -
(47) notes: - no notes -
但随后相同数组中的相同 key-val 对在紧随其后的 foreach 循环中回显(在第 52 行回显),没有发生任何进一步的变量处理, 打印出每个键的空值:
(52) status:
(52) discoverDate:
(52) resolveDate:
(52) ticketIssued:
(52) eqptSystem:
(52) alarmCode:
(52) troubleDescription:
(52) troubleshootingSteps:
(52) resolveActivities:
(52) notes:
不知何故,数组的键值在分配后立即被清零,我不知道如何或为什么。任何人都可以看到可能发生这种情况的明显原因吗?
【问题讨论】:
-
伙计,你用空值
''创建了$troubleInsertInputVarsArr,你期待什么?
标签: php