I'm creating a class that assists in building settings pages for WordPress plugins. The developer instantiates the class by passing in an array of settings. A basic example looks like:
Array
(
[textbox1] => Array
(
[title] => Textbox
[type] => text
[section] => general
[desc] => The text description
)
[a_nice_dropdown] => Array
(
[title] => Select me
[type] => select
[section] => general
[desc] => The select description
[choices] => Array
(
[red] => Red
[blue] => Blue
[green] => Green
)
)
)
This works fine. My class builds the options page and the inputs have HTML that looks like:
<input id="textbox1" type="text" name="options_slug[textbox1]" value="">
When "Save Changes" is clicked, my class grabs all the options tied to "options_slug" and stores them in a single wp_options entry as a nice serialized array, making it easy to grab later.
The New Challenge
I have a new project which requires multiple nested "repeater" fields, similar to the way Advanced Custom Fields handles it. I've created a new field type, to handle this, which can support "subfields". An example config output (from error_log) looks like:
Array
(
[subfields_container] => Array
(
[title] => Subfields
[type] => subfields
[section] => general
[desc] => This is the subfields description text
[subfields] => Array
(
[textbox2] => Array
(
[title] => Textbox
[type] => text
[section] => general
[desc] => The text description
)
[select1deep] => Array
(
[title] => Subfield Select
[type] => select
[choices] => Array
(
[1] => 1
[2] => 2
[3] => 3
)
[std] => 1
)
)
)
)
I've configured the HTML output so an input inside a "subfields" container now looks like:
<input id="textbox1" type="text" name="options_slug[subfields_container][textbox2]" value="">
The idea being that the end user can easily group fields: i.e.,
$options = get_option('options_slug');
foreach($options['subfield_container'] as $subfield) {
// etc...
}
The Problem
As I iterate through these multidimensional arrays, I need to continually update a $options variable at the current index so it can be saved to the DB. So where previously I was able to do:
$id = 'textbox1';
$options[$id] = $_POST['textbox1'];
Now I'm doing something like:
$id = array('subfields_container' => 'textbox2');
$options[$id] = $_POST['textbox2'];
But I get "illegal offset type" errors. Because I can't set an array property using another array.
I've considered just putting dashes in the ID's instead of creating a hierarchical array, something like:
<input id="textbox1" type="text" name="options_slug[subfields_container-textbox2]" value="">
But then I'll lose the ability to foreach over a specific part of the stored options.
The Question
What's the best way to dynamically set a value inside of a multidimensional array when the array is not fixed in structure?
Thank you