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

时间:2016-11-03 16:59:42

标签: php wordpress

为奇怪的标题道歉,不知道如何说出来。基本上我有这样一个数组:

array (
    'key' => 'field_123456',
    'name' => '123456',
),

我将重复使用这一点(对于高级自定义字段),我希望有一种方法可以自动使用'key'中的'name'值,如下所示:

array (
    'key' => 'field_' $name_value_here,
    'name' => '123456',
),

知道这是否可行?我在这上面找不到任何东西。到目前为止,我已经包含了其余的代码示例。

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',
            ),
        ),
    ),
));

1 个答案:

答案 0 :(得分:1)

你不能以这种方式引用同一数组的其他字段,但可能的解决方案是创建一个简单的类,为你这样做:

<?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());