2014-12-04 58 views
-1

我有一個腳本,它循環處理從數據庫表中收集的一堆數據。我已經閱讀了StackOverflow上有關合並重複數組鍵的其他類似帖子,但其中沒有一個似乎適用於我。使用下面的代碼,我建立的所有數據編製成一個數組:合併多維數組中的重複鍵

$sql = 'SELECT * FROM '.$qstTable.' WHERE '.$type.'_qst_id = '.$answer['answer_qst']; 
$result = $db->sql_query($sql); 
$q = $db->sql_fetchrow($result); 
$db->sql_freeresult($result); 

$sql = 'SELECT * FROM '.$catTable.' WHERE '.$type.'_cat_clean = "'.$q[$type.'_qst_cat'].'"'; 
$result = $db->sql_query($sql); 
$cat = $db->sql_fetchrow($result); 
$db->sql_freeresult($result); 

$daField = $cat[$type.'_cat_name']; 

if(count($allQsts)){ 
    if(array_key_exists($daField, $allQsts)){ 
     $daData = array(
      'question' => array(
       'id' => $q[$type.'_qst_id'], 
       'qst' => $q[$type.'_qst_qst'], 
      ), 
      'answer' => array(
       'id' => $answer['answer_id'], 
       'type' => $answer['answer_input'], 
       'content' => $answer['answer_content'], 
       'q_type' => $type, 
      ) 
     ); 
     array_push($allQsts[$daField], $daData); 
    }else{ 
     $allQsts[$cat[$type.'_cat_name']][] = array(
      'question' => array(
       'id' => $q[$type.'_qst_id'], 
       'qst' => $q[$type.'_qst_qst'], 
      ), 
      'answer' => array(
       'id' => $answer['answer_id'], 
       'type' => $answer['answer_input'], 
       'content' => $answer['answer_content'], 
       'q_type' => $type, 
      ) 
     ); 
    } 
}else{ 
    $allQsts[$cat[$type.'_cat_name']][] = array(
     'question' => array(
      'id' => $q[$type.'_qst_id'], 
      'qst' => $q[$type.'_qst_qst'], 
     ), 
     'answer' => array(
      'id' => $answer['answer_id'], 
      'type' => $answer['answer_input'], 
      'content' => $answer['answer_content'], 
      'q_type' => $type, 
     ) 
    ); 
} 

這是我的陣列是如何變成了期待,當這一切都處理:

Array ( 
    [Ancestry] => Array ( 
     [0] => Array ( 
      [question] => Array ( 
       [id] => 1 
       [qst] => Has your family always lived in this country? Where did your family come from? How did they come here? 
      ) 
      [answer] => Array ( 
       [id] => 28 
       [type] => text 
       [content] => idk 
       [q_type] => life 
      ) 
     ) 
    ) 
) 
Array ( 
    [High School] => Array ( 
     [0] => Array ( 
      [question] => Array ( 
       [id] => 158 
       [qst] => Who were your best friends in high school? Were they the same ones from grade school? Do you still keep in touch with them? 
      ) 
      [answer] => Array ( 
       [id] => 30 
       [type] => video 
       [content] => v-0bd3d270-2f24-0132-cd89-12313914f10b 
       [q_type] => life 
      ) 
     ) 
    ) 
) 
Array ( 
    [High School] => Array ( 
     [0] => Array ( 
      [question] => Array ( 
       [id] => 124 
       [qst] => What year did you start high school? What high school did you go to? Did you like it? Did you ever wish you would've gone to a different high school? 
      ) 
      [answer] => Array ( 
       [id] => 36 
       [type] => text 
       [content] => Started HS in 1987 
       [q_type] => life 
      ) 
     ) 
    ) 
) 
Array ( 
    [Young Adult] => Array ( 
     [0] => Array ( 
      [question] => Array ( 
       [id] => 213 
       [qst] => As a young adult did you stay in the same town as your friends or did you move to a new place and had to make new friends? 
      ) 
      [answer] => Array ( 
       [id] => 39 
       [type] => video 
       [content] => v-7d59df50-3bda-0132-cda7-12313914f10b 
       [q_type] => life 
      ) 
     ) 
    ) 
) 
Array ( 
    [Young Adult] => Array ( 
     [0] => Array ( 
      [question] => Array ( 
       [id] => 207 
       [qst] => After high school - did you go to college, join the miltary, or did you get a job? 
      ) 
      [answer] => Array ( 
       [id] => 40 
       [type] => text 
       [content] => went to college at ASU 
       [q_type] => life 
      ) 
     ) 
    ) 
) 
Array ( 
    [Multiple Sclerosis] => Array ( 
     [0] => Array ( 
      [question] => Array ( 
       [id] => 1278 
       [qst] => Do you know of any potential new drugs or treatments that are in development to treat multiple sclerosis? Are you optomistic? 
      ) 
      [answer] => Array ( 
       [id] => 33 
       [type] => text 
       [content] => vg hjc 
       [q_type] => pack 
      ) 
     ) 
    ) 
) 

然而,我希望做的是結合陣列中已經存在的節點,就像這樣:

Array ( 
    [Ancestry] => Array ( 
     [0] => Array ( 
      [question] => Array ( 
       [id] => 1 
       [qst] => Has your family always lived in this country? Where did your family come from? How did they come here? 
      ) 
      [answer] => Array ( 
       [id] => 28 
       [type] => text 
       [content] => idk 
       [q_type] => life 
      ) 
     ) 
    ) 
) 
Array ( 
    [High School] => Array ( 
     [0] => Array ( 
      [question] => Array ( 
       [id] => 158 
       [qst] => Who were your best friends in high school? Were they the same ones from grade school? Do you still keep in touch with them? 
      ) 
      [answer] => Array ( 
       [id] => 30 
       [type] => video 
       [content] => v-0bd3d270-2f24-0132-cd89-12313914f10b 
       [q_type] => life 
      ) 
     ) 
     [1] => Array ( 
      [question] => Array ( 
       [id] => 124 
       [qst] => What year did you start high school? What high school did you go to? Did you like it? Did you ever wish you would've gone to a different high school? 
      ) 
      [answer] => Array ( 
       [id] => 36 
       [type] => text 
       [content] => Started HS in 1987 
       [q_type] => life 
      ) 
     ) 
    ) 
) 
Array ( 
    [Young Adult] => Array ( 
     [0] => Array ( 
      [question] => Array ( 
       [id] => 213 
       [qst] => As a young adult did you stay in the same town as your friends or did you move to a new place and had to make new friends? 
      ) 
      [answer] => Array ( 
       [id] => 39 
       [type] => video 
       [content] => v-7d59df50-3bda-0132-cda7-12313914f10b 
       [q_type] => life 
      ) 
     ) 
     [1] => Array ( 
      [question] => Array ( 
       [id] => 207 
       [qst] => After high school - did you go to college, join the miltary, or did you get a job? 
      ) 
      [answer] => Array ( 
       [id] => 40 
       [type] => text 
       [content] => went to college at ASU 
       [q_type] => life 
      ) 
     ) 
    ) 
) 
Array ( 
    [Multiple Sclerosis] => Array ( 
     [0] => Array ( 
      [question] => Array ( 
       [id] => 1278 
       [qst] => Do you know of any potential new drugs or treatments that are in development to treat multiple sclerosis? Are you optomistic? 
      ) 
      [answer] => Array ( 
       [id] => 33 
       [type] => text 
       [content] => vg hjc 
       [q_type] => pack 
      ) 
     ) 
    ) 
) 

如何修改上面的代碼來做到這一點?

編輯 - 更新,包括一些我試圖陣列壓縮代碼: 創建充分$allQsts陣後,我環度過這段和測試輸出,而每個條目更是複製。

$sortedIt = array(); 
foreach($allQsts as $m => $n){ 
    if(!isset($sortedIt[$m])){ 
     $sortedIt[$m] = array(); 
    } 
    $sortedIt[$m] = $n; 
} 

我也一直在玩弄array_merge_recursive但還沒有得到望其項背。

+1

如果你已經閱讀了幾篇文章,你肯定已經嘗試了一些答案?換句話說,你有什麼嘗試? – 2014-12-04 02:17:46

+0

順便說一句,你的輸出顯示6個​​數組,而不是一個單一的數組;你是如何得到這個輸出的? – 2014-12-04 02:21:26

+0

@Ja͢ck我已更新該帖子以顯示我嘗試過的一些代碼。至於輸出,可能是因爲代碼所在的循環。這是一個可笑的複雜循環結構,可能設計不佳,但也是唯一能讓整個事情起作用的方法。 – chaoskreator 2014-12-04 02:28:21

回答

0

假設這在循環中運行,並且每個時間$allQsts指向相同的數組,就可以大幅度地簡化代碼:

$daField = $cat[$type.'_cat_name']; 
$daData = array(
    'question' => array(
     'id' => $q[$type.'_qst_id'], 
     'qst' => $q[$type.'_qst_qst'], 
    ), 
    'answer' => array(
     'id' => $answer['answer_id'], 
     'type' => $answer['answer_input'], 
     'content' => $answer['answer_content'], 
     'q_type' => $type, 
    ) 
); 

if (array_key_exists($daField, $allQsts)) { 
    $allQsts[$daField][] = $daData; 
} else { 
    $allQsts[$daField] = array($daData); 
} 

這本身應該足以獲得所需的數據結構。

+0

它似乎部分工作,但在其中一個循環結束時,我取消設置'$ allQsts',因爲我在處理循環時遇到了數據倍增,然後三倍等問題。如果你願意,我可以將整個腳本發佈到pastebin或其他東西上,這樣你就可以看一看。就像我說的那樣,這很複雜而且令人費解。 – chaoskreator 2014-12-04 02:39:32

+0

@chaoskreator你必須首先弄清楚它爲什麼是雙倍和三倍。 – 2014-12-04 02:40:39