3v4l.org

run code in 500+ PHP versions simultaneously
<?php function interleave($students, $lessons) { $numStudents = count($students); $numLessons = count($lessons); if ( $numLessons >= $numStudents ) { $studentSliceSize = 1; $lessonSliceSize = intdiv($numLessons, $numStudents); } else { $studentSliceSize = intdiv($numStudents, $numLessons); $lessonSliceSize = 1; } $combinedArray = []; $nextStudent = 0; $nextLesson = 0; while ( $nextStudent < $numStudents || $nextLesson < $numLessons ) { $studentSlice = array_slice($students, $nextStudent, $studentSliceSize); array_push($combinedArray, ...$studentSlice); $nextStudent += $studentSliceSize; $lessonSlice = array_slice($lessons, $nextLesson, $lessonSliceSize); array_push($combinedArray, ...$lessonSlice); $nextLesson += $lessonSliceSize; } return $combinedArray; } var_dump(interleave(['John'], ['Maths'])); var_dump(interleave(['John'], ['Maths', 'Science'])); var_dump(interleave(['John', 'Mary'], ['Maths', 'Science'])); var_dump(interleave(['John', 'Mary', 'Sarah'], ['Maths', 'Science'])); var_dump(interleave(['John', 'Mary', 'Sarah', 'David'], ['Maths', 'Science'])); var_dump(interleave(['John', 'Mary', 'Sarah', 'David', 'Tim'], ['Maths', 'Science'])); var_dump(interleave(['John', 'Mary'], ['Maths', 'Science', 'Music'])); var_dump(interleave(['John', 'Mary'], ['Maths', 'Science', 'Music', 'English']));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8uEGr
function name:  (null)
number of ops:  57
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   34     0  E >   INIT_FCALL                                                   'var_dump'
          1        INIT_FCALL                                                   'interleave'
          2        SEND_VAL                                                     <array>
          3        SEND_VAL                                                     <array>
          4        DO_FCALL                                          0  $0      
          5        SEND_VAR                                                     $0
          6        DO_ICALL                                                     
   36     7        INIT_FCALL                                                   'var_dump'
          8        INIT_FCALL                                                   'interleave'
          9        SEND_VAL                                                     <array>
         10        SEND_VAL                                                     <array>
         11        DO_FCALL                                          0  $2      
         12        SEND_VAR                                                     $2
         13        DO_ICALL                                                     
   37    14        INIT_FCALL                                                   'var_dump'
         15        INIT_FCALL                                                   'interleave'
         16        SEND_VAL                                                     <array>
         17        SEND_VAL                                                     <array>
         18        DO_FCALL                                          0  $4      
         19        SEND_VAR                                                     $4
         20        DO_ICALL                                                     
   38    21        INIT_FCALL                                                   'var_dump'
         22        INIT_FCALL                                                   'interleave'
         23        SEND_VAL                                                     <array>
         24        SEND_VAL                                                     <array>
         25        DO_FCALL                                          0  $6      
         26        SEND_VAR                                                     $6
         27        DO_ICALL                                                     
   39    28        INIT_FCALL                                                   'var_dump'
         29        INIT_FCALL                                                   'interleave'
         30        SEND_VAL                                                     <array>
         31        SEND_VAL                                                     <array>
         32        DO_FCALL                                          0  $8      
         33        SEND_VAR                                                     $8
         34        DO_ICALL                                                     
   40    35        INIT_FCALL                                                   'var_dump'
         36        INIT_FCALL                                                   'interleave'
         37        SEND_VAL                                                     <array>
         38        SEND_VAL                                                     <array>
         39        DO_FCALL                                          0  $10     
         40        SEND_VAR                                                     $10
         41        DO_ICALL                                                     
   42    42        INIT_FCALL                                                   'var_dump'
         43        INIT_FCALL                                                   'interleave'
         44        SEND_VAL                                                     <array>
         45        SEND_VAL                                                     <array>
         46        DO_FCALL                                          0  $12     
         47        SEND_VAR                                                     $12
         48        DO_ICALL                                                     
   43    49        INIT_FCALL                                                   'var_dump'
         50        INIT_FCALL                                                   'interleave'
         51        SEND_VAL                                                     <array>
         52        SEND_VAL                                                     <array>
         53        DO_FCALL                                          0  $14     
         54        SEND_VAR                                                     $14
         55        DO_ICALL                                                     
         56      > RETURN                                                       1

Function interleave:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
2 jumps found. (Code = 47) Position 1 = 51, Position 2 = 53
Branch analysis from position: 51
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 25
Branch analysis from position: 54
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
2 jumps found. (Code = 47) Position 1 = 51, Position 2 = 53
Branch analysis from position: 51
Branch analysis from position: 53
Branch analysis from position: 53
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
filename:       /in/8uEGr
function name:  interleave
number of ops:  56
compiled vars:  !0 = $students, !1 = $lessons, !2 = $numStudents, !3 = $numLessons, !4 = $studentSliceSize, !5 = $lessonSliceSize, !6 = $combinedArray, !7 = $nextStudent, !8 = $nextLesson, !9 = $studentSlice, !10 = $lessonSlice
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
    4     2        COUNT                                                ~11     !0
          3        ASSIGN                                                       !2, ~11
    5     4        COUNT                                                ~13     !1
          5        ASSIGN                                                       !3, ~13
    7     6        IS_SMALLER_OR_EQUAL                                          !2, !3
          7      > JMPZ                                                         ~15, ->15
    8     8    >   ASSIGN                                                       !4, 1
    9     9        INIT_FCALL                                                   'intdiv'
         10        SEND_VAR                                                     !3
         11        SEND_VAR                                                     !2
         12        DO_ICALL                                             $17     
         13        ASSIGN                                                       !5, $17
    7    14      > JMP                                                          ->21
   12    15    >   INIT_FCALL                                                   'intdiv'
         16        SEND_VAR                                                     !2
         17        SEND_VAR                                                     !3
         18        DO_ICALL                                             $19     
         19        ASSIGN                                                       !4, $19
   13    20        ASSIGN                                                       !5, 1
   16    21    >   ASSIGN                                                       !6, <array>
   18    22        ASSIGN                                                       !7, 0
   19    23        ASSIGN                                                       !8, 0
   21    24      > JMP                                                          ->49
   22    25    >   INIT_FCALL                                                   'array_slice'
         26        SEND_VAR                                                     !0
         27        SEND_VAR                                                     !7
         28        SEND_VAR                                                     !4
         29        DO_ICALL                                             $25     
         30        ASSIGN                                                       !9, $25
   23    31        INIT_FCALL                                                   'array_push'
         32        SEND_REF                                                     !6
         33        SEND_UNPACK                                                  !9
         34        CHECK_UNDEF_ARGS                                             
         35        DO_ICALL                                                     
   24    36        ASSIGN_OP                                         1          !7, !4
   26    37        INIT_FCALL                                                   'array_slice'
         38        SEND_VAR                                                     !1
         39        SEND_VAR                                                     !8
         40        SEND_VAR                                                     !5
         41        DO_ICALL                                             $29     
         42        ASSIGN                                                       !10, $29
   27    43        INIT_FCALL                                                   'array_push'
         44        SEND_REF                                                     !6
         45        SEND_UNPACK                                                  !10
         46        CHECK_UNDEF_ARGS                                             
         47        DO_ICALL                                                     
   28    48        ASSIGN_OP                                         1          !8, !5
   21    49    >   IS_SMALLER                                           ~33     !7, !2
         50      > JMPNZ_EX                                             ~33     ~33, ->53
         51    >   IS_SMALLER                                           ~34     !8, !3
         52        BOOL                                                 ~33     ~34
         53    > > JMPNZ                                                        ~33, ->25
   31    54    > > RETURN                                                       !6
   32    55*     > RETURN                                                       null

End of function interleave

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
173.79 ms | 2120 KiB | 25 Q