
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to insert into a table all combination of values in PHP arrays?
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');
- Related Articles
- How to insert values from one table into another in PostgreSQL?
- Auto insert values into a MySQL table in a range?
- How to insert an array of values in a MySQL table with a single INSERT?
- How to insert Date value into table in JDBC?
- How can we insert values into a table with the help of MySQL self-computed output?
- MySQL query for INSERT INTO using values from another table?
- How to insert Binary data into a table using JDBC?
- Which PHP function is used to insert data into an existing MySQL table?
- Split a string and insert it as individual values into a MySQL table?
- How to insert a DATALINK object into a table using JDBC?
- How to write MySQL procedure to insert data into a table?
- Insert JSON into a MySQL table?
- How can we insert data into an existing MySQL table by using PHP script?
- How to insert own values into auto_increment column in MySQL?
- How can we insert data into a MySQL table?

Advertisements