在PHP中使用同一数组中的数组值

在PHP中使用同一数组中的数组值,php,wordpress,Php,Wordpress,为这个奇怪的标题道歉,我不知道该怎么说。基本上我有这样一个数组: array ( 'key' => 'field_123456', 'name' => '123456', ), array ( 'key' => 'field_' $name_value_here, 'name' => '123456', ), <?php class MyAcfObject { public $name; public funct

为这个奇怪的标题道歉,我不知道该怎么说。基本上我有这样一个数组:

array (
    'key' => 'field_123456',
    'name' => '123456',
),
array (
    'key' => 'field_' $name_value_here,
    'name' => '123456',
),
<?php
class MyAcfObject {
    public $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function toArray() {
        return array(
            'field' => 'field_' . $this->name,
            'name' => $this->name
            );
    }
}

$myAcf = new MyAcfObject('example');
$myOtherAcf = new MyAcfObject('differentname');

print_r($myAcf->toArray());
print_r($myOtherAcf->toArray());
对于高级自定义字段,我将大量重复使用此字段,我希望有一种方法可以自动使用“key”中的“name”值,如下所示:

array (
    'key' => 'field_123456',
    'name' => '123456',
),
array (
    'key' => 'field_' $name_value_here,
    'name' => '123456',
),
<?php
class MyAcfObject {
    public $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function toArray() {
        return array(
            'field' => 'field_' . $this->name,
            'name' => $this->name
            );
    }
}

$myAcf = new MyAcfObject('example');
$myOtherAcf = new MyAcfObject('differentname');

print_r($myAcf->toArray());
print_r($myOtherAcf->toArray());
你知道这是否可行吗?我在这上面找不到任何东西。到目前为止,我已经包括了代码的其余部分,作为它的示例

acf_add_local_field_group(array(
    'key' => 'group_header',
    'title' => 'Page Header',
    'fields' => array (
        array (
            'key' => 'field_header_title_tab',
            'label' => 'Title',
            'name' => 'header_title_tab',
            'type' => 'tab',
            'placement' => 'left',
        ),
        array (
            'key' => 'field_header_title',
            'label' => 'Title',
            'instructions' => 'The page title will be used if this field is left empty',
            'name' => 'header_title',
            'type' => 'text',
        ),
        array (
            'key' => 'field_header_subtitle',
            'label' => 'Subtitle',
            'name' => 'header_subtitle',
            'type' => 'text',
        ),
        array (
            'key' => 'field_header_button_tab',
            'label' => 'Button',
            'name' => 'title',
            'type' => 'tab',
            'placement' => 'left',
        ),
    ),
    'position' => 'acf_after_title',
    'label_placement' => 'left',
    'location' => array (
        array (
            array (
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'page',
            ),
        ),
        array (
            array (
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'portfolio',
            ),
        ),
    ),
));

您不能以这种方式引用同一数组的其他字段,但一种可能的解决方案是创建一个简单的类,为您执行此操作,如下所示:

array (
    'key' => 'field_123456',
    'name' => '123456',
),
array (
    'key' => 'field_' $name_value_here,
    'name' => '123456',
),
<?php
class MyAcfObject {
    public $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function toArray() {
        return array(
            'field' => 'field_' . $this->name,
            'name' => $this->name
            );
    }
}

$myAcf = new MyAcfObject('example');
$myOtherAcf = new MyAcfObject('differentname');

print_r($myAcf->toArray());
print_r($myOtherAcf->toArray());
编辑

下面是一个类中有更多字段的示例,您可以在类中设置默认值,以避免始终传递每个字段,结果数组中将不会返回具有null值的字段:

<?php
class MyAcfObject {
    public $data = array(
        'name' => null,
        'label' => null,
        'instructions' => null,
        'type' => 'text',
        'placement' => 'left'
    );

    public function __construct($data = null) {
        if(is_string($data)) {
            $this->data['name'] = $data;
        } elseif(is_array($data)) {
            $this->data = array_merge($this->data, $data);
        }
    }

    public function toArray() {
        $this->data['key'] = 'field_' . $this->data['name'];
        return array_filter($this->data, function($value) { return(!is_null($value)); });
    }
}

$myAcf = new MyAcfObject('myname'); // if you pass a string it will be used as name

$myOtherAcf = new MyAcfObject(array('name' => 'differentname', 'label' => 'My label'));

$evenAnotherAcf = new MyAcfObject(array('name' => 'evendifferentname', 'placement' => 'right'));

print_r($myAcf->toArray());
print_r($myOtherAcf->toArray());
print_r($evenAnotherAcf->toArray());

$myAcf->data['placement'] = 'right'; // you can change values after creating the object

$myAcf->data['placeholder'] = 'myplaceholder'; // you can add fields that are not in the class

print_r($myAcf->toArray());