當前正在與朋友一起處理服務腳本。當我說服務腳本時,我的意思是它只會用於更新和向數據庫添加值。現在我的朋友已經把她所有的數據存儲爲數組了。多個數組 - 將值插入數據庫
我已經成功地進入了其中一個數組,現在我很困惑我該怎麼做多個數組。因此,總而言之,這是一個關於如何將當前代碼重構爲更具動態性的問題。
另外請記住,她有數百個數組,這需要做到,因此它不像2或3它的字面數百。
我更新的代碼:(請閱讀評論)
$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 AllArrays
if(is_array($value) && $varName == 'colors') { // If array is colors then
$Arrays = array_merge($Arrays, $value); // Merge those arrays into the new array
}
}
var_dump($Arrays);
$sql = "INSERT INTO `product_features` ("; // Create the initial query
foreach ($Arrays as $column => $value) { // ForEach over the array
$sql .= "$column,"; // Use the Key Example : 'Colors_All and Color_Bright_All' as a column name
}
$sql2 = rtrim($sql, ","); // trim the initial "," from the columns at the end
$sql2 .= ")"; // Close off the columns names
$sql2 .= " VALUES ";
foreach ($Arrays as $column => $value) { // This is where the problem starts -_-
foreach ($value as $key => $insert) { // Get the value
$sql2 .= "('$insert', '$insert'),"; // I need to have unique values here :(
}
}
$finSQL = rtrim($sql2, ","); // Strip off the remaining ","
而且我知道我不是綁定的參數,我一旦我得到的實際硬的東西出路。
現在做$ finSQL轉儲,當我得到這個:
string(152) "INSERT INTO `product_features` (Colors_All,Colors_Bright_All) VALUES ('Black', 'Black'),('Charcoal', 'Charcoal'),('Silver', 'Silver'),('White', 'White')"
怎樣纔可以有獨特的價值在我插入查詢中的值?這是令我困惑的最後一部分。
你有很多定義的變量在PHP腳本中定義?我不會推薦它,但如果它們不遵循一個明確的模式,你可以嘗試使用'get_defined_vars()'。你能夠展示更多的代碼嗎? – calcinai 2013-04-20 05:04:49
你可以重新輸入你剛輸入的內容嗎? PHP文檔中定義了一堆定義的變量?我會看看get_defined_vars() – 2013-04-20 05:05:44
你似乎缺乏將各種顏色鏈接在一起的明智鑰匙。如果你解釋了最終數據庫應該是什麼樣子,這將有所幫助。 – 2013-04-20 05:14:03