gpt4 book ai didi

php - 数组键值在分配后立即被清空/归零

转载 作者:可可西里 更新时间:2023-11-01 12:58:27 25 4
gpt4 key购买 nike

我有一个使用 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 循环中的输入变量 echo'd(第 47 行的 echo')按我的预期打印出来(输入到表单中的值页面的输入字段):

(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 -

但随后在紧随其后的 foreach 循环中回显相同数组中的相同 key-val 对(在第 52 行回显),没有任何进一步的变量处理已经发生,为每个键打印出空值:

(52) status:
(52) discoverDate:
(52) resolveDate:
(52) ticketIssued:
(52) eqptSystem:
(52) alarmCode:
(52) troubleDescription:
(52) troubleshootingSteps:
(52) resolveActivities:
(52) notes:

不知何故,数组的键值在分配后立即被归零,我不知道如何或为什么。任何人都可以看出为什么会发生这种情况的明显原因吗?

最佳答案

您永远不会为 $troubleInsertInputVarsArr 分配值,只能分配键。你可以试试这个:

   foreach ( $troubleInsertInputVarsArr as $key => $val ) {
$troubleInsertInputVarsArr[$key] = sanitizeText( $_POST[ $key ]);
if ( $key !== 'discoverDate' && $key !== 'resolveDate' ) {
$val = $_SESSION[ $key ] = sanitizeText( $_POST[ $key ] );
echo '<br>(' . __LINE__ . ') ' . $key . ': ' . $val . ' ';
}
}

关于php - 数组键值在分配后立即被清空/归零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43679087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com