【问题标题】:PHP: How to extract() values from an associative array with hyphens/dashes in their keys?PHP:如何从键中带有连字符/破折号的关联数组中提取()值?
【发布时间】:2016-06-23 19:14:44
【问题描述】:

我指的是this question现在是否可以从关联数组中使用连字符/破折号来extract() 值?

这是关于 WordPress Shortcode API 的旧版本。示例:

function bartag_func( $atts ) {
    extract( shortcode_atts( array(
        'foo-bar' => 'something'
    ), $atts ) );

    return "foo = ${foo-bar}";
}
add_shortcode( 'bartag', 'bartag_func' );

【问题讨论】:

  • smacks forehead PHP 仍然没有改变变量命名规则。如果你需要这些值并且你不需要它们在变量中,你可以使用其他一些数组解析方法。

标签: php wordpress extract hyphen


【解决方案1】:

It is still not possible。但是,对于 PHP.net 引擎,我有 an RFC under discussion,这将使 PHP 8 成为可能。

【讨论】:

    【解决方案2】:

    shortcode_atts 返回一个数组,所以只需使用它。

    function bartag_func( $atts ) {
        $params = shortcode_atts( array(
            'foo-bar' => 'something'
        ), $atts ) );
    
        return "foo = " . $params['foo-bar'];
    }
    add_shortcode( 'bartag', 'bartag_func' );
    

    【讨论】:

    • 很好的答案,但它缺乏对导出字段问题的准确答案。
    猜你喜欢
    • 1970-01-01
    • 2015-05-14
    • 2021-04-03
    • 2015-09-11
    • 1970-01-01
    • 2015-05-21
    • 2011-11-26
    • 2013-02-17
    • 2016-12-08
    相关资源
    最近更新 更多