2014-02-11 4 views
2

두 개의 다른 다차원 배열을 병합 할 수 있지만 첫 번째 배열의 키와 값을 두 번째 배열에 복사 할 수 있습니까?두 개의 다차원 배열을 병합하거나 합치십시오

내가 작동하도록하는 방법은 다음과 같습니다. 첫 번째 배열은 입력 필드를 설정합니다. 입력 필드는 복제 가능합니다 (jQuery를 사용하여 복제, 정확하게는 http://jsfiddle.net/8M98y/). 필드 데이터를 저장하면 두 번째 배열이 결과 출력이됩니다. 문제는 두 번째 배열에 저장된 데이터를 올바르게 출력하는 데 필요한 첫 번째 배열의 특정 키와 값이 부족하다는 것입니다.

키 또는 값을 첫 번째 배열에서 두 번째 배열로 복사하는 동안 배열을 조인하거나 병합하는 방법이 있습니까?

이 같은 외모에서 키/값을 복사해야 할 첫 번째 배열 :

Array 
(
    [group-1] => Array 
     (
      [fields] => Array 
       (
        [text-field-1] => Array 
         (
          [name] => Text Field 1 
          [value] => Value 1 
          [comments] => true 
         ) 

        [text-field-2] => Array 
         (
          [name] => Text Field 2 
          [value] => Value 2 
          [comments] => false 
         ) 

       ) 

     ) 

) 

두 번째 배열은 다음과 같습니다

Array 
(
    [group-1] => Array 
     (
      [fields] => Array 
       (
        [text-field-1] => New value here 
        [text-field-2] => New value also 
       ) 

     ) 

    [group-2] => Array 
     (
      [fields] => Array 
       (
        [text-field-1] => Cloned group with new value 
        [text-field-2] => Cloned group with new value also 
       ) 

     ) 

) 

이 두 배열 할 수 있었다 그래서 경우에 병합 된 배열의 출력이 다음과 같이 표시되어야합니다. http://pastebin.com/uzuZs73B

나는 단지 array_merge($array2, $array1)을 사용하려고했지만 결과 출력은 다음과 같습니다 : http://pastebin.com/DucKGMN3 실제로 병합되지만 키와 값은 복사되지 않습니다.

편집 : 여기에 사례를 설명해야합니다.

어떻게 작동합니까? 저장되지 않은 초기 출력은 첫 번째 배열에 의해 생성 된 그룹의 두 개의 텍스트 입력입니다. 이 그룹은 jQuery 복제본을 사용하여 복제 가능합니다 (이 예제는 여기 : http://jsfiddle.net/8M98y/). 따라서 하나 이상의 그룹을 추가/복제 한 다음 저장하면 결과로 저장된 데이터가 두 번째 배열이됩니다. 두 번째 배열의 문자열은 실제 저장된 입력 값입니다. 그것들은 첫 번째 배열에서 [value]로 들어갈 것입니다.

그러나, 필드의 출력은 여전히 ​​의미 제 배열에 기초 그것을 출력 할 수 없다가 배열하지 않은 상기 제 배열에서 같은 키와 값을 가지고 있지 않기 때문에 제대로 클로닝 기 .

enter image description here

사람이 몇 가지 통찰력을 제공 할 수있는 경우

, 그것은 거대하고 크게 감상 할 수있다.

내가 질문을 잘못 이해하지만, 요구 사항을 병합하는 경우
+0

두 번째 배열의 문자열의 의미는 무엇입니까? 첫 번째 배열의 특정 키와 일치합니까? – baldrs

+0

해당 문자열은 입력의 저장된 값입니다. 나는 그걸 넣었어야했다.미안합니다. –

답변

1

죄송합니다? 당신이 두 배열에 액세스 할 수있는 경우 당신은 당신이가는대로 원래의 키 값을 매핑하고 새 값으로 덮어 쓰기, 두 번째 배열을 반복 할 수있다.

귀하의 배열 :

$arr1 = Array 
(
    'group-1' => Array 
     (
      'fields' => Array 
       (
        'text-field-1' => Array 
         (
          'name' => 'Text Field 1', 
          'value' => 'Value 1', 
          'comments' => 'true' 
         ), 

        'text-field-2' => Array 
         (
          'name' => 'Text Field 2', 
          'value' => 'Value 2', 
          'comments' => 'false' 
         ) 

       ) 

     ) 

); 


$arr2 = Array 
(
    'group-1' => Array 
     (
      'fields' => Array 
       (
        'text-field-1' => 'New value here', 
        'text-field-2' => 'New value also' 
       ) 

     ), 

    'group-2' => Array 
     (
      'fields' => Array 
       (
        'text-field-1' => 'Cloned group with new value', 
        'text-field-2' => 'Cloned group with new value also' 
       ) 

     ) 

); 

비밀 소스 :

foreach($arr2 as $k=>$v){ 
    // Get the new values for this iteration 
    $val1 = $arr2[$k]['fields']['text-field-1']; 
    $val2 = $arr2[$k]['fields']['text-field-2']; 
    // Duplicate the original array 
    $arr2[$k]['fields'] = $arr1['group-1']['fields']; 
    // Insert the new values 
    $arr2[$k]['fields']['text-field-1']['value'] = $val1; 
    $arr2[$k]['fields']['text-field-2']['value'] = $val2; 
} 


echo '<pre>'; 
print_r($arr2); 
echo '</pre>'; 

exit(); 

반환

Array 
(
    [group-1] => Array 
     (
      [fields] => Array 
       (
        [text-field-1] => Array 
         (
          [name] => Text Field 1 
          [value] => New value here 
          [comments] => true 
         ) 

        [text-field-2] => Array 
         (
          [name] => Text Field 2 
          [value] => New value also 
          [comments] => false 
         ) 

       ) 

     ) 

    [group-2] => Array 
     (
      [fields] => Array 
       (
        [text-field-1] => Array 
         (
          [name] => Text Field 1 
          [value] => Cloned group with new value 
          [comments] => true 
         ) 

        [text-field-2] => Array 
         (
          [name] => Text Field 2 
          [value] => Cloned group with new value also 
          [comments] => false 
         ) 

       ) 

     ) 

)