从php文件中的所有数组变量获取值
我有一个php文件(shortcode config.php),其中声明了一些var数组 我想为这个文件中的每个var获取每个数组中的一些值 shortcode-config.php:从php文件中的所有数组变量获取值,php,arrays,var,Php,Arrays,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_
$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
中定义的唯一一个(我不知道如何在文件中循环,而不知道$theme\u shortcode['']
中的名称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将是三分之一
,二分之一
等。。$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'];
}
}
文件是否包括在内?你想得到什么?文件确实包含正确。我可以访问它们。我想获取所有var
$theme\u短代码[]
名称,例如,数组popup\u title
shortcode\u图标中的每个名称。目前,我将$popup
var推入我的函数,并在shortcode-config.phph文件中正确地获取了一个var。我想在不使用$popup var的情况下自动执行此操作…所以您想为每个$theme\u短代码项创建theme\u sc\u数据实例,对吗?是的。目前,我需要知道变量的名称。多亏了我的函数theme\u sc\u data
中的$popup
var,他们才能推送它。我想在文件中循环并获取所有$theme\u短代码[]
var和数组参数,而不知道我的var$theme\u短代码[]
的名称。谢谢您的回答。但我不明白如何输出echo,例如$val['popup_title'代码>?foreach($key=>$val){echo$val['title'];}
很抱歉,这是我的错误!它当然很好用!谢谢!
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);
}
}
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'];
}
}