-2

Each array hold different type of exam with same student Id.second array value push first array receptive value .

Array 1

Array ( 
    [0] => Array ( [student_id] => 1190101 [stname] => Student 1 [remarks] => [totals] => 21 ) 
    [1] => Array ( [student_id] => 1190102 [stname] => Student 2 [remarks] => 1 [totals] => 6 ) 
    [2] => Array ( [student_id] => 1190103 [stname] => Student 3 [remarks] => 
        [totals] => 11 ) 
    [3] => Array ( [student_id] => 1190104 [stname] => Student 4 [remarks] => 1 [totals] => 6 ) 
    [4] => Array ( [student_id] => 1190105 [stname] => Student 5 [remarks] => 1 [totals] => 6 ) 
    [5] => Array ( [student_id] => 1190106 [stname] => Student 6 [remarks] => 1 [totals] => 6 ) 
    [6] => Array ( [student_id] => 1190107 [stname] => Student 7 [remarks] => 1 [totals] => 6 ) 
    [7] => Array ( [student_id] => 1190108 [stname] => Student 8 [remarks] => 1 [totals] => 6 ) 
    [8] => Array ( [student_id] => 1190109 [stname] => Student 9 [remarks] => 1 [totals] => 6 ) 
    [9] => Array ( [student_id] => 1190110 [stname] => Student 10 [remarks] => 1 [totals] => 6 ) )

array 2

Array ( 
    [0] => Array ( [student_id] => 1190104 [stname] => Student 4 [remarks] => [totals] => 5 ) 
    [1] => Array ( [student_id] => 1190105 [stname] => Student 5 [remarks] => [totals] => 5 ) )

array 3

Array (
    [0] => Array ( [student_id] => 1190106 [stname] => Student 6 [remarks] => [totals] => 5 ) 
    [1] => Array ( [student_id] => 1190107 [stname] => Student 7 [remarks] => [totals] => 5 )
    [2] => Array ( [student_id] => 1190108 [stname] => Student 8 [remarks] => [totals] => 5 ) 
)

Need Single multidimensional array Like

Array ( 
    [0] => Array ( [student_id] => 1190101 [stname] => Student 1 [remarks] => 1 [totals] => 21 ) 
    [1] => Array ( [student_id] => 1190102 [stname] => Student 2 [remarks] => 1 [totals] => 6 ) 
    [2] => Array ( [student_id] => 1190103 [stname] => Student 3 [remarks] => [totals] => 11 ) 
    [3] => Array ( [student_id] => 1190104 [stname] => Student 4 [remarks] => 1 [totals] => 6*[totals] => 5* ) 
    [4] => Array ( [student_id] => 1190105 [stname] => Student 5 [remarks] => 1 [totals] =>4 [totals] => 5 ) 
    [5] => Array ( [student_id] => 1190106 [stname] => Student 6 [remarks] => 1 [totals] => 6[totals] => 5 ) 
    [6] => Array ( [student_id] => 1190107 [stname] => Student 7 [remarks] => 1 [totals] => 6 [totals] => 5 ) 
    [7] => Array ( [student_id] => 1190108 [stname] => Student 8 [remarks] => 1 [totals] => 6 ) 
    [8] => Array ( [student_id] => 1190109 [stname] => Student 9 [remarks] => 1 [totals] => 6 [totals] => 5 ) 
    [9] => Array ( [student_id] => 1190110 [stname] => Student 10 [remarks] => 1 [totals] => 6 ) 
)
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • It's hard to see what's going on here, but I would iterate through each of the arrays in turn and dump the contents into a new array, and in the new array I'd use the student ID as the key. That would make it easy to make sure that everything for each student to wind up in his/her variable – Stevish Feb 25 '22 at 17:09
  • It does help if the question is readable. – RiggsFolly Feb 25 '22 at 17:40
  • WHat did you intend here `[totals] => 6*[totals] => 5* ` as that is not a valid array !??? – RiggsFolly Feb 25 '22 at 17:46

1 Answers1

0

You could iterate through each array and put all the data into a new array, using student ID as the key:

$new_array = [];
$old_arrays = [$array1, $array2, $array3];

foreach( $old_arrays as $old_array ) {
    foreach( $old_array as $key => $value ) {
        $new_array[ $old_array['student_id'] ][$key] = $value;
    }
}

So each time it finds a student that was already added, it will add the information again. If array2 has a piece of data on student #123 that array1 didn't have, it will be added to student #123's array in the new array by the key.

This assumes that each student ID is correct, and there is only one student per ID and one ID per student, and that none of the other info (like stname or remarks) are conflicting in different arrays.

Stevish
  • 734
  • 5
  • 17
  • In your example, you show in your single array that student 1190106 has [total] => 6 AND [total] 5. Would that be what you want? Or would you want only one total? And if you want only one, would you want the larger, the smaller, or to add them together? All that could be done by adding specific `if ( $key === 'total' )` conditions to change the behavior. – Stevish Feb 25 '22 at 17:18
  • Yes i want this format student 1190106 has [total] => 6 AND [total] 5. – Nithya Kalyani Feb 25 '22 at 17:47
  • That as said above is not possible as its not a valid array – RiggsFolly Feb 25 '22 at 17:48
  • You woudl have to make `totals` an array of totals `[total] => Array ( 6, 5)` – RiggsFolly Feb 25 '22 at 17:49