问题描述
我在 wp_options
表中有数据存储为 multi-dimensional 阵列 (profile_element_order
):
a:12:{s:17:"img_base64_enable";s:1:"1";s:25:"moulding_combination_page";s:0:"";s:24:"moulding_collection_page";s:0:"";s:25:"idea_gallery_thumb_height";s:3:"200";s:24:"idea_gallery_thumb_width";s:3:"200";s:23:"collection_thumb_height";s:3:"200";s:22:"collection_thumb_width";s:3:"200";s:20:"profile_item_columns";s:1:"4";s:17:"idea_item_columns";s:1:"2";s:24:"collections_item_columns";s:1:"2";s:25:"combinations_item_columns";s:1:"4";s:21:"profile_element_order";a:5:{i:0;s:8:"Option 1";i:1;s:8:"Option 2";i:2;s:8:"Option 3";i:3;s:8:"Option 4";i:4;s:8:"Option 5";}}
我想要完成的是更新 profile_element_order
选项 (在这些选项中) 。以下是一切如何:
function psort_save_order() {
global $mouldings_options;
$list = $mouldings_options['profile_element_order'];
$new_order = $_POST['list_items'];
$new_list = array();
// update order
foreach($new_order as $v) {
if(isset($list[$v])) {
$new_list[$v] = $list[$v];
}
}
// save the new order
update_option('profile_element_order', $new_list);
die();
}
add_action('wp_ajax_psort_update_order', 'psort_save_order');
数据正确地发布到 DB 表 (我可以看到我的一些失败的尝试作为新的选项条目,如 mouldings_settings->profile_element_order
) – 我只是很难找出 update_option()
语法的具体选项。我已经尝试过了 (请注意,mouldings_settings 是实际的选项名称):
mouldings_settings['profile_element_order']
$mouldings_options['profile_element_order']
profile_element_order
但现在没有骰子。任何指针都将不胜感激!谢谢!
更新这是我现在所做的 – ajax 动作保存不错,但是当我保存插件选项时,它会复制数据库中的选项,并引发与之前相同的错误:
a:17:{s:17:"img_base64_enable";s:1:"1";s:25:"moulding_combination_page";s:0:"";s:24:"moulding_collection_page";s:0:"";s:25:"idea_gallery_thumb_height";s:3:"200";s:24:"idea_gallery_thumb_width";s:3:"200";s:23:"collection_thumb_height";s:3:"200";s:22:"collection_thumb_width";s:3:"200";s:20:"profile_item_columns";s:1:"4";s:17:"idea_item_columns";s:1:"2";s:24:"collections_item_columns";s:1:"2";s:25:"combinations_item_columns";s:1:"4";s:21:"profile_element_order";a:5:{i:4;s:8:"Option 5";i:0;s:8:"Option 1";i:1;s:8:"Option 2";i:3;s:8:"Option 4";i:2;s:8:"Option 3";}i:0;s:8:"Option 5";i:1;s:8:"Option 1";i:2;s:8:"Option 2";i:3;s:8:"Option 4";i:4;s:8:"Option 3";}
功能:
function psort_save_order() {
global $mouldings_options;
$list = $mouldings_options['profile_element_order'];
$new_order = $_POST['list_items'];
$new_list = array();
// update order
foreach($new_order as $v) {
if(isset($list[$v])) {
$new_list[$v] = $list[$v];
}
}
$mouldings_options['profile_element_order'] = $new_list;
$mouldings_options = array_merge($mouldings_options,$mouldings_options['profile_element_order']);
// save the new order
update_option('mouldings_settings', $mouldings_options);
die();
}
add_action('wp_ajax_psort_update_order', 'psort_save_order');
最佳解决方案
至于 WordPress,您的 multi-dimensional 阵列是一个选择。
要仅更新 multi-dimensional 阵列的一部分,它必须检索整个数组,相应地进行更改,然后更新整个数组。
假设您的 multi-dimensional 阵列如下:
my_options = array(
'option_a'=>'value_a',
'option_b'=>'value_b',
'inner_array'=>array(
'foo' => 'bar',
'hello' => 'world',
),
'option_c'=>'value_c'
)
并且假设要将’hello’ 选项的值从’world’ 更新为’moon’
//Get entire array
$my_options = get_option('my_options');
//Alter the options array appropriately
$my_options['inner_array']['hello'] = 'moon';
//Update entire array
update_option('my_options', $my_options);
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。