現在、友人とサービス スクリプトに取り組んでいます。サービス スクリプトとは、データベースへの値の更新と追加にのみ使用されることを意味します。今、私の友人はすぐにすべての彼女をデータベースの値として配列として保存しました。
私はすでに配列の 1 つを入力することに成功していますが、複数の配列に対してどのようにそれを行うことができるかについて混乱しています。全体として、これは現在のコードをより動的なものにリファクタリングする方法についての質問です。
また、心に留めておいてください、彼女はこれを行う必要がある数百の配列を持っているので、文字通り数百の 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')"
挿入クエリで一意の値を VALUES にするにはどうすればよいですか? それが私を混乱させている最後の部分です。