2013-10-22 60 views
0

對不起,使標題聽起來很複雜。數組合並問題

我期待將一個數組合併到一個已經創建的大規模多維數組中。

第一個想法...是否可以創建一個子數組作爲數組();同時循環你創建的數組?

foreach ($records as $record) { 
     $record['AddedContent']['Comments'] = array(); //doesn't currently exist 
      foreach ($comments as $comment) { 
       if($record['AddedContent']['id'] = $comment['content_id']){ 
        array_push($record['AddedContent']['Comments'], $comment); 

       } 
      } 
     } 

這裏評論是小數組我想加入到記錄(大陣列)

陣列如下:

[0] => Array 
    (
     [SubscribedToProfile] => Array 
      (
       [id] => 
       [user_id] => 
       [company_name] => 
       [picture_filename] => 
      ) 

     [AddedContent] => Array 
      (
       [text] => 
       [file_location] => 
       [content_type_id] => 4 
       [file_name] => IxnAvLKMtIU 
       [id] => 87 
      ) 

    ) 

意見數組是:

Array 
(
    [0] => Array 
    (
     [ContentComment] => Array 
      (
       [id] => 3 
       [content_id] => 87 
       [text] => jhbuoijniknb 

       [created] => 2013-10-21 13:08:12 
       [user_id] => 2097 
      ) 

    ) 

[1] => Array 
    (
     [ContentComment] => Array 
      (
       [id] => 4 
       [content_id] => 88 
       [text] => gfrgfrtegvrtegvrtgvrtvtrgv 
       [created] => 2013-10-21 13:08:12 
       [user_id] => 2097 
      ) 
    ) 

,我需要它是:

[0] => Array 
    (
     [SubscribedToProfile] => Array 
      (
       [id] => 
       [user_id] => 
       [company_name] => 
       [picture_filename] => 
      ) 

     [AddedContent] => Array 
      (
       [text] => 
       [file_location] => 
       [content_type_id] => 4 
       [file_name] => IxnAvLKMtIU 
       [id] => 87 
       [comments] => Array 
          (
          [0] => Array 
            (
            [id] => 2 
            [content_id] => 87 
            [text] => fderwcfrvfcerwfvrev 
            [created] => 2013-10-21 13:05:58 
            [user_id] => 2097 
            ) 
          [1] => Array 
            (
            [id] => 3 
            [content_id] => 87 
            [text] => jhbuoijniknb 
            [created] => 2013-10-21 13:08:12 
            [user_id] => 2097 
            ) 
           ) 
        ) 
      ) 

我需要遍歷bpth陣列但正如我需要有條件地加入HTEM看到其中ID是87(在這個例子中)和內容ID是87.

回答

0

您的解決方案就在這裏。

foreach ($records as $key=>$record) { 
    $array = array(); 
    if($key=='AddedContent') { 
     foreach($comments as $comment) { 
      if($record['id'] == $comment["ContentComment"]['content_id']){ 
       array_push($array, $comment["ContentComment"]); 
      } 
     } 
     $records[$key]['comments'] = $array; 
    } 
}