Daniel Post Daniel Post - 1 month ago 5
PHP Question

Using an array value inside the same array in PHP

Apologies for the weird title, wasn't sure how to word it. Basically I have an array like this:

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


I'll be reusing this a ton (for Advanced Custom Fields), and I was hoping there's a way to automatically use the 'name' value inside the 'key', like this:

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


Any idea if this is possible? I couldn't find anything on this. I've included the rest of the code so far as an example of what it will look like.

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

Answer

you can't reference other fields of the same array this way but a possibile solution would be to create a simple class that does this for you like this:

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

Edit

Here's an example with more fields available in the class, you can set default values in the class in order to avoid passing every field all the times, fields with null value will not be returned in the resulting array:

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