![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to insert into a table all combination of values in PHP arrays?\n
For this, use the foreach loop and insert into statement in order to insert all combinations of values in PHP arrays.
Let’s say we have the following arrays −
$name1 = ["John", "Mike"]; $name2 = ["David", "Sam"]; $name3 = ["Bob", "Adam"];
Example
<!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>
Output
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');
Advertisements