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 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)