var 배열을 선언 한 PHP 파일 (shortcode-config.php)이 있습니다.
이 파일의 각 변수에 대해 각 배열의 일부 값을 얻고 싶습니다.
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 in이라는 이름을 모르고 파일 내부를 반복하는 방법을 모르겠습니다 $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
각 변수에 대해 다음과 같은 결과를 출력 하고 싶습니다.
<span><i class="fa <?php echo $theme_shortcodes[$this->popup]['shortcode_icon']; ?>"></i><?php echo $theme_shortcodes[$this->popup]['popup_title']; ?></span>
이 모든 변수의 목록을 해당 속성으로 자동 채우기 위해이 작업을 수행하고 싶습니다.
이 시도:
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);
}
}
여기에 $ 키는 것 one_third
, one_half
등 및 $ 발 배열을 개최한다. 이제 $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] 삭제
몇 마디 만하겠습니다