0

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!

0

1 Answer 1

2

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
)
Sign up to request clarification or add additional context in comments.

2 Comments

How to append this marged arrays to json file?
What happened when you tried that? ;-) (But yes... that looks right)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.