如何在 PHP 数组中插入所有值的组合?
为此,请使用 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');
"; 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');
广告