Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress get_option(),索引键来自字符串_Php_Wordpress_String - Fatal编程技术网

Php Wordpress get_option(),索引键来自字符串

Php Wordpress get_option(),索引键来自字符串,php,wordpress,string,Php,Wordpress,String,--编辑-- 我制作了一个管理表单,在我的主题中添加了一些自定义函数。加载设置页面时,我首先从数据库获取当前设置。为此,我使用get\u option() 设置字段称为product\u settings 要获取此设置的所有值,可以使用以下命令调用它:$option=get_option('product_settings') 其结果相当于: $option = [ 'product_01' => [ 'style' => [

--编辑--

我制作了一个管理表单,在我的主题中添加了一些自定义函数。加载设置页面时,我首先从数据库获取当前设置。为此,我使用
get\u option()

设置字段称为
product\u settings
要获取此设置的所有值,可以使用以下命令调用它:
$option=get_option('product_settings')

其结果相当于:

    $option = [
        'product_01' => [
            'style' => [
                'color' => [
                    'primary' => '#ffffff'
                ]
            ]
        ]
    ];
现在,要获得索引'primary'的值,我将其称为:

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

$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] ];
欢迎提供任何帮助。

您可以这样做:

$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]];
}
$value='product_settings[product_01][style][color][primary];
//以下结果应为:“产品设置[product_01[style[color[primary”
$key_string=str_replace(']','$value);
//使用“[”作为分隔符,为所有值创建一个数组
$key_数组=分解(“[”,$key_字符串);
/*删除第一个值(选项名称),并保存
值(在本例中为“产品设置”)*/
$option\u name=array\u shift($key\u array);
//从数据库中获取选项值(数组):
$option\u settings=get\u option($option\u name);
//将$option\u设置设置为整个返回数组:
$option\u setting=$option\u settings;
/*
在密钥数组中迭代尽可能多的密钥,
更改$option\u设置,使其在每次迭代时更加精确
在获得所需的价值之前:
*/
对于($i=0;$i
$选项设置现在应包含所需的值。

您可以执行以下操作:

$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]];
}
$value='product_settings[product_01][style][color][primary];
//以下结果应为:“产品设置[product_01[style[color[primary”
$key_string=str_replace(']','$value);
//使用“[”作为分隔符,为所有值创建一个数组
$key_数组=分解(“[”,$key_字符串);
/*删除第一个值(选项名称),并保存
值(在本例中为“产品设置”)*/
$option\u name=array\u shift($key\u array);
//从数据库中获取选项值(数组):
$option\u settings=get\u option($option\u name);
//将$option\u设置设置为整个返回数组:
$option\u setting=$option\u settings;
/*
在密钥数组中迭代尽可能多的密钥,
更改$option\u设置,使其在每次迭代时更加精确
在获得所需的价值之前:
*/
对于($i=0;$i
$option setting
现在应该包含您需要的值。

我建议不要尝试直接从
get\u 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 );
我建议不要尝试直接从
get_option()
result()中获取它,而是获取值,然后解析路径

您可以编写自己的函数来实现所需的功能。例如:

注意:这有许多“防御”措施,因此,如果您请求不存在的路径,或使用格式错误的路径,这将不会引发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 );

您试图获取的数据库中的实际选项名称是什么?(在不知道最终目标是什么的情况下,很难帮助您从一个解析到另一个)谢谢@cale_b,我编辑了我的问题。有点混乱您试图获取的数据库中的实际选项名称是什么?(在不知道最终目标是什么的情况下,很难帮助你从一个解析到另一个)谢谢@cale_b,我编辑了我的问题。有点混乱,:)编辑后的一个很有魅力。感谢@markmoxx也感谢代码中的注释。从中学到了一些好东西。是的,在编辑之前,我不小心在循环中为你的键使用了错误的数组。我添加了部分注释,以确保我的逻辑有意义(我没有测试!)当然,为了您的利益:)是的,:)编辑后的注释很有魅力。感谢@markmoxx也感谢代码中的注释。从中学到了一些好东西。是的,在编辑之前,我无意中在循环中为您的键使用了错误的数组。我添加了部分注释,以确保我的逻辑有意义(我没有测试!)当然,为了您的利益:)这是一个非常全面的答案。对于我这个新手来说,这是一个非常有见地的答案。谢谢@cale_bGlad,我可以帮上忙!谢谢您的评论。我已经将foreach循环中的else改为break;返回FALSE;我发现它返回了它的父数组/值,而它搜索的索引没有(还没有)返回它的父数组/值存在。这是一个很好的选择。我选择了
break;
不确定您对该场景的期望处理,因此您所说的是有意义的。这是一个非常全面的答案。对于我这个新手来说,非常有洞察力。谢谢@cale\u bGlad我可以帮助您!谢谢您的评论。我已将foreach循环中的else从break;更改为return FALSE;当它搜索的索引不存在时,我发现它返回了它的父数组/值。这是一个很好的选择。我选择了
break;
不确定您希望对该场景进行的处理,所以您所说的是有意义的。