从php文件中的所有数组var获取值

怪异的

我有一个php文件(shortcode-config.php),在其中声明了一些var数组。

我想为该文件中的每个var获取每个数组中的一些值。

shortcode-config.php:

$theme_shortcodes['one_half'] = array( 
    'no_preview' => true,
    'title'=>__('One Half (1/2)', 'textdomain' ), 
    'shortcode' => '[theme_one_half boxed="{{boxed}}" centered_text="{{centered_text}}" last_column="{{last_column}}"] {{content}} [/themeone_one_half]',
    'popup_title' => __('One Half (1/2) column', 'textdomain'),
    'shortcode_icon' => __('fa-list')
);

$theme_shortcodes['one_third'] = array( 
    'no_preview' => true,
    'title'=>__('One third (1/3)', 'textdomain' ), 
    'shortcode' => '[theme_one_third boxed="{{boxed}}" centered_text="{{centered_text}}" last_column="{{last_column}}"] {{content}} [/themeone_one_half]',
    'popup_title' => __('One third (1/3) column', 'textdomain'),
    'shortcode_icon' => __('fa-list')
);

在我的另一个php文件中,我得到了var,但是目前是我在其中定义的唯一文件$popup(我不知道如何在var中命名var,因此不知道如何在文件中循环$theme_shortcodes['']):

    <?php
    class theme_sc_data {

    var $conf;
    var $popup;
    var $shortcode;
    var $popup_title;
    var $has_child;

    function __construct( $popup ) {
        $this->conf = dirname(__FILE__) . '/shortcodes-config.php';
        $this->popup = $popup;
        $this->formate_shortcode();
    }
    function formate_shortcode() {
        require_once( $this->conf );
        $this->shortcode = $theme_shortcodes[$this->popup]['shortcode'];
        $this->popup_title = $theme_shortcodes[$this->popup]['popup_title'];
        $this->shortcode_icon = $theme_shortcodes[$this->popup]['shortcode_icon'];
    }
}
$popup = 'one_half';
$shortcode = new theme_sc_data($popup);
?>

我想从shortcode-config.php每个var这样的输出

<span><i class="fa <?php echo $theme_shortcodes[$this->popup]['shortcode_icon']; ?>"></i><?php echo $theme_shortcodes[$this->popup]['popup_title']; ?></span>

我想要执行此操作以自动将所有这些var的列表填充为其属性

诺帕尔

试试这个:

require_once( dirname(__FILE__) . '/shortcodes-config.php');
$shortcode = array();
if(!empty($theme_shortcodes)){
   foreach($theme_shortcodes as $key=>$val){
         $shortcode[] = new theme_sc_data($key,$val); 
   }
}

这里$ key将是one_thirdone_half等等。$ val将保存数组。现在$shortcode将是一个对象数组。我认为$key在您的类函数中不重要,但是如果您需要使用它,仍然可以将其传递给您。

并更改您的班级以避免再次包含

class theme_sc_data {

    var $conf;
    var $key;
    var $shortcode_icon;
    var $popup_title;
    var $has_child;

    function __construct( $key,$val ) {
        $this->conf = dirname(__FILE__) . '/shortcodes-config.php';
        $this->key = $key; // actually of no use now, but may be in future
        $this->formate_shortcode($val);
    }
    function formate_shortcode($val) {
        $this->shortcode = $val['shortcode'];
        $this->popup_title = $val['popup_title'];
        $this->shortcode_icon = $val['shortcode_icon'];
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从文件中获取所有数字并将其输入到数组中?

来自分类Dev

PHP-如何扩展数组中的所有数组

来自分类Dev

Postgres从jsonb数组中获取值匹配的所有元素

来自分类Dev

从Angular的本地存储中获取所有数组

来自分类Dev

如何从数组中获取所有数据?

来自分类Dev

如何从 vue 响应中获取所有数组值

来自分类Dev

如果数组可以有任何大小和嵌套,如何从PHP数组中的特定键获取所有数据?

来自分类Dev

在PHP中从数组获取值

来自分类Dev

从PHP数组中获取值

来自分类Dev

通过php非数组从json文件中获取值

来自分类Dev

从带有数组的 json 文件中获取数据

来自分类Dev

如何在带有数组的选择框中获取值

来自分类Dev

PHP-如何获取n个深层数组的所有数组值?

来自分类Dev

PHP-如何获取n个深层军事数组的所有数组值?

来自分类Dev

php,当数组的某些成员缺少键时获取所有数组键

来自分类Dev

PHP在多维数组中获取值

来自分类Dev

PHP仅从数组中获取值

来自分类Dev

从PHP中的数组获取值的索引

来自分类Dev

从PHP中的Json数组获取值

来自分类Dev

从PHP中的Json数组获取值

来自分类Dev

PHP仅从数组中获取值

来自分类Dev

PHP - 在循环中从数组中获取值

来自分类Dev

循环从 php 数组中获取值

来自分类Dev

如何使用php从数组中获取值

来自分类Dev

使用 PHP 从 JSON 数组中获取值

来自分类Dev

从数组中获取值 PHP - 非法偏移

来自分类Dev

PHP 从 JSON 数组中获取值

来自分类Dev

php foreach 多维/嵌套数组获取所有数据

来自分类Dev

如何在codeigniter中的where子句中获取数组中的所有数据