gpt4 book ai didi

php - 如何合并两个数组数据并保存一张SQL表

转载 作者:行者123 更新时间:2023-11-29 10:31:35 24 4
gpt4 key购买 nike

我有两个数组。我想使用 php 合并并插入到数据库。这里我放了一些数组数据。但我的数组非常大。

数组1

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
)
)

数组2

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
)
)

我希望将这两个数据提交到一个表中。这是相应的 SQL 查询。

INSERT INTO MODULE_REG (STUDENT_ID, COURSE_ID, DATE_REGISTERED, REGISTERED, ACADEMIC_SESSION)

我想像这样保存我的数据库表。这是样本一。我必须像这样提交这两个数组的所有数据。

 +-----------+----------+-----------+----------------+-------------+
|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 |
+-----------+----------+-----------+----------------+-------------+

最佳答案

你可以尝试这样的事情

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*/
}
}

我看不到 Registered 和 data_registered 的任何值,因此我没有添加。您可以相应地添加。

关于php - 如何合并两个数组数据并保存一张SQL表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47322038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com