为此,请使用foreach循环并插入到语句中,以便在PHP数组中插入值的所有组合。
假设我们有以下数组-
$name1 = ["John", "Mike"]; $name2 = ["David", "Sam"]; $name3 = ["Bob", "Adam"];
<!DOCTYPE html> <html> <body> <?php $name1 = ["John", "Mike"]; $name2 = ["David", "Sam"]; $name3 = ["Bob", "Adam"]; foreach ($name1 as $n1) { foreach ($name2 as $n2) { foreach ($name3 as $n3) { echo "insert into `student_details` (`name1`, `name2`, `name3`) VALUES ('$n1', '$n2','$n3');\n"; echo "<br>"; } } } ?> </body> </html>
输出结果
INSERT INTO 'student_details'('name1','name2','name3')VALUES('John','David','Adam'); INSERT INTO 'student_details'('name1','name2','name3')VALUES('John','Sam','Bob'); INSERT INTO 'student_details'('name1','name2','name3')VALUES('John','Sam','Adam'); INSERT INTO 'student_details'('name1','name2','name3')VALUES('Mike','David','Bob'); INSERT INTO 'student_details'('name1','name2','name3')VALUES('Mike','David','Adam'); INSERT INTO 'student_details'('name1','name2','name3')VALUES('Mike','Sam','Bob'); INSERT INTO 'student_details'('name1','name2','name3')VALUES('Mike','Sam','Adam');