当前正在与朋友一起处理服务脚本。当我说服务脚本时,我的意思是它只会用于更新和向数据库添加值。现在我的朋友已经把她所有的数据存储为数组了。多个数组 - 将值插入数据库
我已经成功地进入了其中一个数组,现在我很困惑我该怎么做多个数组。因此,总而言之,这是一个关于如何将当前代码重构为更具动态性的问题。
另外请记住,她有数百个数组,这需要做到,因此它不像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