繁体   English   中英

更新关联数组的特定键的值

[英]Update a value of a specific key of an associative array

我正在尝试使用表单更新关联数组的特定键的值。

这是我的表格的外观:

<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循环,这里没有提及: 如何在PHP中更改特定关联数组的值?

有人可以帮我吗?

谢谢

您的代码实际上将数组($ add_to_ID => $ note)添加到$ existing_list数组的每个元素中,但是它用索引322更改了该元素。请尝试如下操作:

foreach ($existing_list as $key => $value) {
    if (isset($value[$add_to_ID])) {
        $existing_list[$key][$add_to_ID] = $note;
        break;
    }    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM