带有来自字符串

时间:2018-12-10 16:56:05

标签: php wordpress string

-已编辑-

我制作了一个管理表单,该表单向主题添加了一些自定义功能。加载设置页面首先,我从数据库中获取当前设置。为此,我使用get_option()

设置字段称为product_settings 要从此设置获取所有值,您可以使用以下命令进行调用:$option = get_option('product_settings');

其结果与此等效:

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

现在,要获取索引“ primary”的值,我会这样称呼它:

从数据库:

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

从数组:

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

现在,一切正常。但是现在是棘手的部分。索引位置以这样的字符串值传递:

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

第一部分是db字段。紧随其后的部分是嵌套索引。

我的问题 如何根据字符串中的索引获取此数组的嵌套值?

这是我到目前为止的尝试:

$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] ];

欢迎任何帮助。

2 个答案:

答案 0 :(得分:1)

您可以这样做:

$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现在应该包含您需要的值。

答案 1 :(得分:1)

我建议不要尝试直接从get_option()结果中获取它,而是获取该值,然后解析路径。

您可以编写自己的函数来实现所需的功能。像这样:

注意:这有很多“防御”措施,因此,如果您请求的路径不存在或使用格式错误的路径,则不会抛出PHP通知/错误:

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

用法:

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