doulian7305 2018-12-10 16:56
浏览 52
已采纳

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.

  • 写回答

2条回答 默认 最新

  • douganmo1121 2018-12-10 18:28
    关注

    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 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化