如何在类内的多维数组中插入一个变量?

问题描述 投票:0回答:1

我正在编辑一个Woocommerce功能,以显示不同语言的不同文本。我对对象不熟悉,因此求助。

我需要在一个多维数组中插入一个变量,但由于它是一个对象,我无法通过array_push来实现。

我的目标是将$comments_placeholder的值插入到数组键 "placeholder "中。

这是目前的代码。

    $this->fields = array(
        'billing'  => WC()->countries->get_address_fields(
            $billing_country,
            'billing_'
        ),
        'shipping' => WC()->countries->get_address_fields(
            $shipping_country,
            'shipping_'
        ),
        'account'  => array(),
        'order'    => array(
            'order_comments' => array(
                'type'        => 'textarea',
                'class'       => array( 'notes' ),
                'label'       => __( 'Order notes', 'woocommerce' ),
                'placeholder' => '',
        ),
    );

array_push($this["order"]["order_comments"]["placeholder"]) = $comments_placeholder;

谢谢!

php arrays wordpress object woocommerce
1个回答
0
投票
$this->fields['order']['order_comments']['placeholder']=$comments_placeholder

$this->field指的是对象的数组,而不是$this,后者指的是对象本身。由于'占位符'是一个键,而不是一个数组,所以不使用array_push,因为array_push会将一个值附加到一个数组中,你可以将$comments_placeholder的值赋值给'占位符'这个键。

© www.soinside.com 2019 - 2024. All rights reserved.