Reputation: 35
I created SUBJECTS TO BE TAKEN BY STUDENTS in "subject" table
TABLE "SUBJECT"
ID| SHORTNAME | SUBJECT_NAME
1 | MT | MATHEMATICS
2 | ASC | ADDITIONAL SCIENCE
The columns in this table are automatically generated into the table "MID_YEAR_EXAMINATION"
TABLE "MID_YEAR_EXAMINATION"
ID | STUDENT NAME | MT | ASC
The scores obtained by students in each subject are inserted in the table "MID_YEAR_EXAMINATION"
ID | STUDENT_NAME | MT | ASC
1 | ALEX | 88 | 62
2 | ELLY | 78 | 43
MY QUESTION is i have two arrays 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);
how to fix that arrays as follows with edit that code:
FROM
[
"id" => "1",
"name" => "ALEX",
"MT" => "88"
]
[
"id" => "1",
"name" => "ALEX",
"ASC" => "62"
]
TO
[
"id" => "1",
"name" => "ALEX",
"MT" => "88",
"ASC" => "62"
]
("MT" and "ASC" merge in one array)
I need your help. Thanks!
Upvotes: 0
Views: 79
Reputation: 3897
Use PHP's array_merge()
function: https://www.php.net/manual/en/function.array-merge.php
If the source arrays are assigned to $a and $b, it's simply $combined = array_merge( $a, $b );
If $b has a different value for the same-named element in $a, the value from $b will win out.
$a = [
"id" => "1",
"name" => "ALEX",
"MT" => "88"
];
$b = [
"id" => "1",
"name" => "ALEX",
"ASC" => "62"
];
print_r( array_merge( $a, $b ) );
output:
Array
(
[id] => 1
[name] => ALEX
[MT] => 88
[ASC] => 62
)
Upvotes: 2