我在“主题”表中创建了要由学生选择的主题
表格“主题”
ID| SHORTNAME | SUBJECT_NAME
1 | MT | MATHEMATICS
2 | ASC | ADDITIONAL SCIENCE
此表中的列自动生成到表“ MID_YEAR_EXAMINATION”中
表“ MID_YEAR_EXAMINATION”
ID | STUDENT NAME | MT | ASC
学生在每个主题中获得的分数将插入表“ MID_YEAR_EXAMINATION”中
ID | STUDENT_NAME | MT | ASC
1 | ALEX | 88 | 62
2 | ELLY | 78 | 43
我的问题是我有两个数组 code2
$input1= array(
"id" => $row['ID'],
"name" => $row['STUDENT_NAME']
);
$sel_query3="Select * from SUBJECT ORDER BY SUBJECT_NAME";
$result3 = @mysqli_query($con,$sel_query3);
while($row2 = @mysqli_fetch_assoc($result3)){
$MP = $row2['SHORTNAME'];
$m = $row['SUBJECT_NAME']; //TO GET student's score
in each subject
$input2 = array($MP => $m);
}
}
Echo json_encode($input2);
如何通过编辑代码来修复该数组,如下所示:
从
[
"id" => "1",
"name" => "ALEX",
"MT" => "88"
]
[
"id" => "1",
"name" => "ALEX",
"ASC" => "62"
]
TO
[
"id" => "1",
"name" => "ALEX",
"MT" => "88",
"ASC" => "62"
]
(“ MT”和“ ASC”合并为一个数组)
我需要你的帮助。谢谢!
答案 0 :(得分:3)
使用PHP的array_merge()
函数:https://www.php.net/manual/en/function.array-merge.php
如果将源数组分配给$ a和$ b,则只需$combined = array_merge( $a, $b );
如果$ b中$ a中相同名称的元素具有不同的值,则$ b中的值将获胜。
$a = [
"id" => "1",
"name" => "ALEX",
"MT" => "88"
];
$b = [
"id" => "1",
"name" => "ALEX",
"ASC" => "62"
];
print_r( array_merge( $a, $b ) );
输出:
Array
(
[id] => 1
[name] => ALEX
[MT] => 88
[ASC] => 62
)