2014-11-09 2 views
0

에 평면 PHP 배열로 변환 나는 다음과 같은 배열 구조를 가지고 :다차원 배열

[parents] => Array 
    (
     [0] => Array 
      (
       [id] => 1 
       [user_id] => 1 
       [created] => 2014-11-09 13:47:37 
       [content] => This is a test discussion 
       [status] => 1 
       [parent] => 0 
       [project_id] => 1 
      ) 

     [1] => Array 
      (
       [id] => 4 
       [user_id] => 1 
       [created] => 2014-11-09 13:52:02 
       [content] => 456789 
       [status] => 1 
       [parent] => 0 
       [project_id] => 1 
      ) 

    ) 

[children] => Array 
    (
     [0] => Array 
      (
       [id] => 2 
       [user_id] => 1 
       [created] => 2014-11-09 13:47:53 
       [content] => This is a test reply.... 
       [status] => 1 
       [parent] => 1 
       [project_id] => 1 
      ) 

     [1] => Array 
      (
       [id] => 3 
       [user_id] => 1 
       [created] => 2014-11-09 13:48:13 
       [content] => This is a test reply....!!! 
       [status] => 1 
       [parent] => 1 
       [project_id] => 1 
      ) 

     [2] => Array 
      (
       [id] => 5 
       [user_id] => 1 
       [created] => 2014-11-09 13:52:17 
       [content] => 8765432 
       [status] => 1 
       [parent] => 4 
       [project_id] => 1 
      ) 

    ) 

다음과 같이 보이는, 그래서 내가 부모/자식 관계로 병합하고 싶습니다 :

[parents] => Array 
    (
     [0] => Array 
      (
       [id] => 1 
       [user_id] => 1 
       [created] => 2014-11-09 13:47:37 
       [content] => This is a test discussion 
       [status] => 1 
       [parent] => 0 
       [project_id] => 1 
       [children] => Array 
        (
         [0] => Array 
          (
           [id] => 2 
           [user_id] => 1 
           [created] => 2014-11-09 13:47:53 
           [content] => This is a test reply.... 
           [status] => 1 
           [parent] => 1 
           [project_id] => 1 
          ) 

         [1] => Array 
          (
           [id] => 3 
           [user_id] => 1 
           [created] => 2014-11-09 13:48:13 
           [content] => This is a test reply....!!! 
           [status] => 1 
           [parent] => 1 
           [project_id] => 1 
          ) 
         ) 
      ) 
    ) 

방법 PHP로 그렇게 할 수 있을까요? 모든 id의 당신의 parents 배열에서 고유한지 가정

+1

당신은'array_merge '로 인터넷 검색을하셨습니까? – HddnTHA

+0

children 배열을 반복하고 결과를 적절한 부모 요소에 추가함으로써 - 지금까지 무엇을 시도 했습니까? – Steve

+0

나는 그런 식으로 글을 쓰려고했지만 부모 배열에 자식 배열을 추가하는 방법을 생각할 때 막혔다. 부모가 아닌 인덱스를 기반으로 추가해야하므로 색인이 아닙니다. – Adam

답변

1

,이 작업을 수행 할 수 있습니다

// build a new array using parent's ID values as the key, 
// to simplify searching for parent IDs. 
foreach ($parents as $key => $value){ 
    $merged[$value['id']] = $value; 
} 

foreach ($children as $child){ 
    $parentID = $child['parent']; 
    if (array_key_exists($parentID, $merged)) { 
     // add the child array to its parent's ['children'] value 
     $merged[$parentID]['children'][] = $child; 
    } 
} 

을 결과 배열 $merged, 각 상위 항목의 키는 그 id로 설정하고, 모든 어린이됩니다 해당 부모 아래에 중첩됩니다.

+0

와우, 아주 좋아! 훌륭한! – Adam