Update Option Stored in Multi-Dimensional Array

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

Leave a Comment