Wordpress get_option(),索引键来自string

--Edited--

I've made a admin form which adds some custom functions to my theme. Loading the settings page I first get the current settings from the database. For this I use get_option().

The setting field is called product_settings To get all values from this setting you can call it with: $option = get_option('product_settings');

The result of this is equivalent to this:

    $option = [
        'product_01' => [
            'style' => [
                'color' => [
                    'primary' => '#ffffff'
                ]
            ]
        ]
    ];

Now, to get the value of index 'primary' I would call it like this:

From DB:

$optionColorPrimary = get_option('product_settings')['product_01']['style']['color']['primary'];

From array:

$optionColorPrimary = $option['product_01']['style']['color']['primary'];

Now, this work all fine and that. But now comes the tricky part. The index location is passed in a string value like this:

$get_option_srt = 'product_settings[product_01][style][color][primary]';

First part is the db field. And the part after it, separated by square brackets are the nested indexes.

My Question How do I get to the nested value of this array based on the indexes from the string?

This is my attempt so far:

$get_option_srt = 'product_settings[product_01][style][color][primary]';

// split in two, to separate the field name from the indexes.
$get_option_srt  = preg_split('/(\[.*\])/', $get_option_srt, 2, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);

// yes, this does not work... 
// The part after get_option() is wrong. Here are the nested index locations needed
$option = get_option( $get_option_srt[0] )[ $get_option_srt[1] ];

Any help is welcome.

I would recommend not attempting to get it directly off the get_option() result, but rather get the value, then parse the path.

You can write your own function that accomplishes what you want. Something like so:

NOTE: This has many "Defensive" measures in place, so that if you ask for a path that doesn't exist, or use a malformed path, this will not throw PHP notices / errors:

function get_option_by_path( $path ) {
    // get all the "keys" from within the square braces
    preg_match_all( '/\[(.+?)\]/', $path, $matches );
    // get the initial "key" (eg, 'product_settings')
    $key = explode( '[', $path );
    // ensure base key is set, in case there were no square braces
    $key = ( ! empty( $key[0] ) ) ? $key[0] : $key;
    // load the option value from the DB
    $option = get_option( $key );
    if ( ! $option || ! is_array( $option ) ) {
        return FALSE;
    }

    // if the passed-in path didn't have any square-brace keys, return the option
    if ( empty( $matches[1] ) ) {
        return $option;
    }

    // loop over all the keys in the square braces
    foreach ( $matches[1] AS $key ) {
        // if $option is an array (still), and has the path, set it as the new $option value
        if ( is_array( $option ) && array_key_exists( $key, $option ) ) {
            $option = $option[ $key ];
        } else {
            // otherwise, can't parse properly, exit the loop
            break;
        }
    }

    // return the final value for the $option value
    return $option;
}

Usage:

$get_option_srt = 'product_settings[product_01][style][color][primary]';
$value = get_option_by_path( $get_option_srt );

You could do this:

$value = 'product_settings[product_01][style][color][primary]';
// The below should result in: 'product_settings[product_01[style[color[primary'
$key_string = str_replace(']', '', $value);
//Create an array for all the values, using '[' as the delimiter
$key_array = explode('[', $key_string);
/* Remove first value (the option name), and save the 
value (which is 'product_settings' in this case) */
$option_name = array_shift($key_array);
// Get the option value (an array) from the db:
$option_settings = get_option($option_name);

// Set $option_setting to be the entire returned array:
$option_setting = $option_settings;

/*
Iterate through your key array for as many keys as you have, 
changing $option_setting to be more refined on each iteration 
until you get the value you need:
*/
for ($i = 0; $i < count($key_array); $i++) {
    $option_setting = $option_setting[$key_array[$i]];
}

$option setting should now contain the value you need.