当前位置: 首页>>技术教程>>正文


更新选项存储在Multi-Dimensional阵列中

webfans 技术教程 , , , 去评论

问题描述

我在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');

数据正确地发布到数据库表(因为我可以看到我的一些失败的尝试作为新的选项条目,如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);

参考资料

本文由朵颐IT整理自网络, 文章地址: https://duoyit.com/article/2647.html,转载请务必附带本地址声明。