2016-11-03 4 views
1

이상한 제목에 사과드립니다. 어떻게 말할지는 잘 모릅니다. 기본적으로이 같은 배열이 있습니다PHP에서 동일한 배열 내부의 배열 값 사용

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

내가 (고급 사용자 정의 필드에 대한)이 톤을 재사용 할 수 있습니다를, 나는 자동으로 '키'내부의 '이름'값을 사용하는 방법이 좋겠습니다 예 :

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

이 같은 당신을 위해이 작업을 수행하는 간단한 클래스를 생성하는 것 같은 배열이 방법 만 possibile 솔루션의 다른 필드를 참조 할 수 없습니다 :

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