我有一个类,我需要构建一个数组。我通常可以通过查询和循环来做到这一点,但我不知道如何使用类。
我是否先预先创建数组并将其转换为字符串?
这是我的code sample
/** My Setting class **/
if ( ! class_exists( 'WC_my_setting' ) ) {
class WC_my_setting {
public function __construct() {
// Init settings
$this->settings = array(
array(
'name' => __( 'My Setting', 'woocommerce-my-setting' ),
'type' => 'title',
'id' => 'wc_my_setting_options'
),
array(
'name' => __( 'Name', 'woocommerce-my-setting' ),
'id' => 'wc_my_setting_category',
'type' => 'multiselect',
//I want to have this->settings[options] populated with the array that the $category_options makes.
'options' => $category_options
),
)
),
array( 'type' => 'sectionend', 'id' => 'woocommerce-my-setting' ),
);
// Default options
add_option( 'wc_my_setting_category', '' );
// Admin
add_action( 'woocommerce_settings_image_options_after', array( $this, 'admin_settings' ), 20 );
add_action( 'woocommerce_update_options_catalog', array( $this, 'save_admin_settings' ) );
add_action( 'woocommerce_update_options_products', array( $this, 'save_admin_settings' ) );
}
我可以在页面模板下面的代码就好了,我可以输出数组,但我不能在管理区域的类中使用它:
$categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0' );
$category_options = array();
if ( $categories ) {
foreach ( $categories as $cat ) {
$category_options[ $cat->slug ] = $cat->name;
}
}
// print_r($category_options);
如何在我的课程中获得上述循环,以便将$category_options
放在$this->settings[options]
答案 0 :(得分:1)
试试这个:
<?php
class Test
{
var $TestArray = array();
function Test($inputArray)
{
$this->setTestArray($inputArray);
$this->Display();
}
?>
答案 1 :(得分:0)
class nameClass { public $ settings = array();
public function __construct()
{
$this->settings = array('id' => '10','name' => 'Peter');
}
}
$ objSettings = new nameClass();
的var_dump($ objSettings-&gt;设置);
的var_dump($ objSettings-&gt;设置);
你能得到吗?