【发布时间】:2014-03-23 12:05:09
【问题描述】:
我正在尝试使用表单更新关联数组的特定键的值。
这是我的表单的外观:
<form class="wpd_edit_note_322" method="post" action="">
<input type="hidden" id="list_note" name="list_note" value="ba222f06db">
<p><textarea id="plugn_note" name="plugin_note" placeholder="Note(Optional)" ></textarea></p>
<p><input class="list_note_id" name="plugin_note_id" type="hidden" value="322"></p>
<p><input class="list_edit_button" type="submit" value="Save"></p>
</form>
这就是我在提交时尝试更新键 322 的值的方式:
if ( isset( $_POST['list_note'] ) && wp_verify_nonce($_POST['list_note'],'edit_item_note') )
{
$add_to_ID = $_POST['plugin_note_id'];
$note = $_POST['plugin_note'];
$existing_list = Array (
[0] => Array ( [320] => This is the plugin that I am using on my site. )
[1] => Array ( [322] => My Microblog poster bla blah bla. )
[2] => Array ( [318] => )
);
foreach ( $existing_list as $k => $v ) {
$existing_list[$k][$add_to_ID] = $note;
}
}
当我回显它时,我看到了 $_POST 值。所以我猜这个表单可以正常工作,但是 foreach 循环不能正常工作。
我还尝试使用 array_walk_recursive() 代替 foreach 循环,此处提到但无济于事:
How to change the value of an specific associative array in PHP?
有人可以帮帮我吗?
谢谢
【问题讨论】:
-
$existing_list的数组语法不正确。这是您的实际代码吗? -
$existing_list 数组中的每一行后都需要逗号
-
数组列表实际上是
print_r版本的get_post_meta($postid, 'my_list_items', TRUE )我刚刚添加了打印版本来显示数组。我应该使用var_dump输出吗?谢谢。