Reputation: 35
I have two array. I want merge and insert to the database using php. here i put some array data. but my array is very big one.
Array1
Array
(
[1] => Array
(
[ALT_ID] => 2003211
[STUDENT_ID] => 2235
[CUSTOM_30] => JAN
[TITLE] => Level 4
)
[2] => Array
(
[ALT_ID] => 2003458
[STUDENT_ID] => 2495
[CUSTOM_30] => JAN
[TITLE] => Level 4
)
[3] => Array
(
[ALT_ID] => 2003211
[STUDENT_ID] => 2235
[CUSTOM_30] => JAN
[TITLE] => Level 4
)
[4] => Array
(
[ALT_ID] => 2003587
[STUDENT_ID] => 2624
[CUSTOM_30] => JAN
[TITLE] => Level 4
)
[5] => Array
(
[ALT_ID] => 2003614
[STUDENT_ID] => 2651
[CUSTOM_30] => JAN
[TITLE] => Level 4
)
[6] => Array
(
[ALT_ID] => 2003682
[STUDENT_ID] => 2721
[CUSTOM_30] => JAN
[TITLE] => Level 4
)
[7] => Array
(
[ALT_ID] => 2003685
[STUDENT_ID] => 2724
[CUSTOM_30] => JAN
[TITLE] => Level 4
)
[8] => Array
(
[ALT_ID] => 2003777
[STUDENT_ID] => 2814
[CUSTOM_30] => JAN
[TITLE] => Level 4
)
)
Array2
Array
(
[1] => Array
(
[COURSE_ID] => 1
[TITLE] => Programming Methodology
[SHORT_NAME] => ECSI402
[MODULE_STATUS] => Core
)
[2] => Array
(
[COURSE_ID] => 2
[TITLE] => Communication and Learning Skills in Computer Science (SE)
[SHORT_NAME] => ECSI400
[MODULE_STATUS] => Core
)
[3] => Array
(
[COURSE_ID] => 3
[TITLE] => Computer Systems Fundamentals
[SHORT_NAME] => ECSI404
[MODULE_STATUS] => Core
)
[4] => Array
(
[COURSE_ID] => 4
[TITLE] => Information and Data Modeling (SE)
[SHORT_NAME] => EBSI401
[MODULE_STATUS] => Core
)
[5] => Array
(
[COURSE_ID] => 5
[TITLE] => Software Development Principles
[SHORT_NAME] => ECSI406
[MODULE_STATUS] => Core
)
)
I want these two data submit to the one table. this is the SQL query for that.
INSERT INTO MODULE_REG (STUDENT_ID, COURSE_ID, DATE_REGISTERED, REGISTERED, ACADEMIC_SESSION)
My database table i want save like this. this is sample one. i have to submit these two arrays all data for like this.
+-----------+----------+-----------+----------------+-------------+
|student id | course id| registered| data_registered| academic ses|
+-----------+----------+-----------+----------------+-------------+
| 2235 | 1 | Y | 2017.11.16 | Level 4 |
| 2235 | 2 | Y | 2017.11.16 | Level 4 |
| 2235 | 3 | Y | 2017.11.16 | Level 4 |
| 2495 | 1 | Y | 2017.11.16 | Level 4 |
| 2495 | 2 | Y | 2017.11.16 | Level 4 |
| 2495 | 3 | Y | 2017.11.16 | Level 4 |
+-----------+----------+-----------+----------------+-------------+
Upvotes: 0
Views: 122
Reputation: 35
This is the answer. $result
is array1
..
$result1
is array2
foreach ($result as $value){
$value[STUDENT_ID];
$value[TITLE];
foreach($result1 as $values){
$values[COURSE_ID];
$date = date("Y-m-d");
$sql= "INSERT INTO MODULE_REGISTRATION (STUDENT_ID, COURSE_ID,REGISTERED, DATE_REGISTERED, ACADEMIC_SESSION) "
. "VALUES ('".$value[STUDENT_ID]."','".$values[COURSE_ID]."', 'Y' ,'".$date. "','".$value[TITLE]."')";
DBQuery($sql);
}
}
Upvotes: 0
Reputation: 3080
You can try something like this
for($i=0;$i<count($array1);$i++){
$student_id[$i]=$array1[$i]['STUDENT_ID'];
$accademic_ses[$i]=$array1[$i]['ACADEMIC_SESSION'];
for($j=0;$j<count($array2);$j++){
$sql= "INSERT INTO MODULE_REG (STUDENT_ID, COURSE_ID, DATE_REGISTERED, REGISTERED, ACADEMIC_SESSION) VALUES (".$student_id[$i].",".$course_id[$j].",/*add your registered and data_registered here/*,".$accademic_ses[$i].")";
/*execute your sql here*/
}
}
I cannot see any values for registered and data_registered so I didnt add. You can add accordingly.
Upvotes: 1