Update Option Stored in Multi-Dimensional Array

I have data in the wp_options table currently stored as a multi-dimensional array (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";}}

What I’m trying to accomplish is update the profile_element_order option (within those options). Here’s how everything looks so far:

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

The data is correctly posting to the DB table (as I can see some of my failed attempts as new option entries, like mouldings_settings->profile_element_order) — I’m just having a hard time figuring out the update_option() syntax for just that specific option. I’ve tried things like (bearing in mind `mouldings_settings is the actual option name):

mouldings_settings['profile_element_order']
$mouldings_options['profile_element_order']
profile_element_order

but no dice at the moment. Any pointers would be greatly appreciated! Thanks!

Update
This is what I have now — ajax action saves fine, but when I save the plugin options, it duplicates the options in the database and throws up the same error as before:

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:

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

As far WordPress is concerned – your multi-dimensional array is one option.

To update just part of the multi-dimensional array its necessary to retrieve the entire array, alter it accordingly and then update the entire array.

Suppose your multi-dimensional array is as follows:

my_options = array(
  'option_a'=>'value_a',
  'option_b'=>'value_b',
  'inner_array'=>array(
       'foo' => 'bar',
       'hello' => 'world',
   ),
  'option_c'=>'value_c'
)

And suppose you want to update the value of the ‘hello’ option from ‘world’ to ‘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);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x