3v4l.org

run code in 300+ PHP versions simultaneously
<?php $correctAnswers = [ ['name'=> 1, 'value'=> 4], ['name'=> 1, 'value'=> 1], ['name'=> 1, 'value'=> 3], ['name'=> 1, 'value'=> 1], ['name'=> 1, 'value'=> 2] ]; $submittedStudentData =[ [101, 0, 1, 1, 0, 0, 0], [102, 0, 4, 1, 3, 1, 2], [103, 0, 4, 1, 0, 0, 0], [104, 0, 0, 1, 0, 0, 0], [105, 0, 0, 0, 0, 0, 0] ]; if(!is_array($submittedStudentData)) { $submittedStudentData = json_decode($submittedStudentData); } $s_data = []; for ($index = 0, $c = count($submittedStudentData); $index < $c; $index++){ // get each student data $studentData = $submittedStudentData[$index]; // get the student roll number $studentId = $studentData[0]; // initialize the total result with zero pre value $totalResult = 0; // loop through the student's submitted answer for ($i = 2, $x = count($studentData) ; $i < $x; $i++) { if ((int)$studentData[$i] === (int)$correctAnswers[$i - 2]["value"]) { $totalResult++; } } $s_data[] = ['studentId' =>$studentId, 'scores'=>$totalResult]; // final result for this student } usort($s_data, function($k, $v) { return $v['scores'] <=> $k['scores']; }); foreach($s_data as $d) { echo 'Student with roll number '. $d['studentId'] .' scores '. $d['scores']."\n"; } ?>
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 42, Position 2 = 14
Branch analysis from position: 42
2 jumps found. (Code = 77) Position 1 = 48, Position 2 = 57
Branch analysis from position: 48
2 jumps found. (Code = 78) Position 1 = 49, Position 2 = 57
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
Branch analysis from position: 57
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 57
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
2 jumps found. (Code = 44) Position 1 = 35, Position 2 = 23
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 42, Position 2 = 14
Branch analysis from position: 42
Branch analysis from position: 14
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 32
Branch analysis from position: 31
2 jumps found. (Code = 44) Position 1 = 35, Position 2 = 23
Branch analysis from position: 35
Branch analysis from position: 23
Branch analysis from position: 32
Branch analysis from position: 9
filename:       /in/amfBa
function name:  (null)
number of ops:  59
compiled vars:  !0 = $correctAnswers, !1 = $submittedStudentData, !2 = $s_data, !3 = $index, !4 = $c, !5 = $studentData, !6 = $studentId, !7 = $totalResult, !8 = $i, !9 = $x, !10 = $d
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   10     1        ASSIGN                                                   !1, <array>
   18     2        TYPE_CHECK                                  128  ~13     !1
          3        BOOL_NOT                                         ~14     ~13
          4      > JMPZ                                                     ~14, ->9
   20     5    >   INIT_FCALL                                               'json_decode'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $15     
          8        ASSIGN                                                   !1, $15
   23     9    >   ASSIGN                                                   !2, <array>
   24    10        ASSIGN                                                   !3, 0
         11        COUNT                                            ~19     !1
         12        ASSIGN                                                   !4, ~19
         13      > JMP                                                      ->40
   27    14    >   FETCH_DIM_R                                      ~21     !1, !3
         15        ASSIGN                                                   !5, ~21
   30    16        FETCH_DIM_R                                      ~23     !5, 0
         17        ASSIGN                                                   !6, ~23
   34    18        ASSIGN                                                   !7, 0
   37    19        ASSIGN                                                   !8, 2
         20        COUNT                                            ~27     !5
         21        ASSIGN                                                   !9, ~27
         22      > JMP                                                      ->33
   39    23    >   FETCH_DIM_R                                      ~29     !5, !8
         24        CAST                                          4  ~30     ~29
         25        SUB                                              ~31     !8, 2
         26        FETCH_DIM_R                                      ~32     !0, ~31
         27        FETCH_DIM_R                                      ~33     ~32, 'value'
         28        CAST                                          4  ~34     ~33
         29        IS_IDENTICAL                                             ~30, ~34
         30      > JMPZ                                                     ~35, ->32
   41    31    >   PRE_INC                                                  !7
   37    32    >   PRE_INC                                                  !8
         33    >   IS_SMALLER                                               !8, !9
         34      > JMPNZ                                                    ~38, ->23
   45    35    >   INIT_ARRAY                                       ~40     !6, 'studentId'
         36        ADD_ARRAY_ELEMENT                                ~40     !7, 'scores'
         37        ASSIGN_DIM                                               !2
         38        OP_DATA                                                  ~40
   24    39        PRE_INC                                                  !3
         40    >   IS_SMALLER                                               !3, !4
         41      > JMPNZ                                                    ~42, ->14
   51    42    >   INIT_FCALL                                               'usort'
         43        SEND_REF                                                 !2
         44        DECLARE_LAMBDA_FUNCTION                          ~43     [0]
   53    45        SEND_VAL                                                 ~43
   51    46        DO_ICALL                                                 
   55    47      > FE_RESET_R                                       $45     !2, ->57
         48    > > FE_FETCH_R                                               $45, !10, ->57
   57    49    >   FETCH_DIM_R                                      ~46     !10, 'studentId'
         50        CONCAT                                           ~47     'Student+with+roll+number+', ~46
         51        CONCAT                                           ~48     ~47, '+scores+'
         52        FETCH_DIM_R                                      ~49     !10, 'scores'
         53        CONCAT                                           ~50     ~48, ~49
         54        CONCAT                                           ~51     ~50, '%0A'
         55        ECHO                                                     ~51
   55    56      > JMP                                                      ->48
         57    >   FE_FREE                                                  $45
   60    58      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/amfBa
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $k, !1 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   52     2        FETCH_DIM_R                                      ~2      !1, 'scores'
          3        FETCH_DIM_R                                      ~3      !0, 'scores'
          4        SPACESHIP                                        ~4      ~2, ~3
          5      > RETURN                                                   ~4
   53     6*     > RETURN                                                   null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.22 ms | 1008 KiB | 15 Q