2013-04-20 68 views
3

首先關於這個question這個問題。我的問題是,我的一個朋友有大約300左右的陣列,她需要插入數據庫。我收到了數據庫部分,正如你在問題中注意到的那樣,我將這部分關聯起來。然而,我的問題出現在我應該如何獲得所有數組並將它們放在一起,以便我可以對數組執行foreach並檢查值是否爲數組,然後使用數組名作爲INSERT查詢中的表。如何組合多個陣列

這是我更新的代碼:

 $colors['Colors_All'] = array("Black","Charcoal"); // Add unique indexes 
     $colors['Colors_Bright_All'] = array("Silver","White"); // Add unique indexes 

     $AllArrays = get_defined_vars(); // Get all defined vars 
     $Arrays = array(); // Set a default array 

     foreach ($AllArrays as $varName => $value) { // Run through all the variables set in the get_defined_vars 
      if(is_array($value) && $varName == 'colors') { // If it is an array and if the array is colors[] then 
       $Arrays = array_merge($Arrays, $value); // Merge those arrays into the new array 
      } 
     } 

現在,這會給我訪問所有的數據。

+1

你能告訴你有什麼到目前爲止一些片斷?只是這樣我們才能看到你使用的代碼是如何工作的 – pjongjang 2013-04-20 14:56:32

+0

爲什麼你的朋友不會將這些硬編碼數組存儲在數據庫中?然後,他們將不需要循環遍歷所有數組,以在插入它之前檢查值... – Havelock 2013-04-20 14:57:51

+0

這就是我們想要處理的數組,我們要將它們存儲在數據庫中。它只是非常規的做300或表(每個陣列的表),然後插入每個陣列20或30個值...你知道我的意思嗎? – 2013-04-20 14:59:47

回答

2

在這裏你去:

$colors['Colors_All']  = array("Black","Charcoal","Light_Gray","Silver","White","Gold","Bronze","Copper","Platinum","Navy","Royal_Blue","Dodger_Blue","Deep_Sky_Blue","Turquoise","Tiffany_Blue"); 
$colors['Colors_Bright_All'] = array("Silver","White","Gold","Royal_Blue","Dodger_Blue","Deep_Sky_Blue","Deep_Green","Forest_Green","Bright_Green","Violet"); 
$colors['Colors_Light_All'] = array("Light_Gray","Silver","White","Gold","Dodger_Blue","Deep_Sky_Blue","Light_Blue","Bright_Green","LightGreen","Light_Green"); 

// This will store the merged results of each array 
$colorVars = array(); 

// Loop through all of the defined variables 
foreach ($colors as $colorKey => $value) { 
    // Add the results of this array to the main $colorVars array 
    $colorVars = array_merge($colorVars, $value); 
} 
+0

布蘭登我將無法測試/接受這個答案,直到我接觸到她。所以不要以爲我只是無視這個回答。 – 2013-04-20 15:05:39

+0

@RixhersAjazi好吧,讓我知道它是如何工作 – 2013-04-20 15:06:22

+0

幾乎100%的工作更新我的OP – 2013-04-20 23:15:20