3v4l.org

run code in 500+ PHP versions simultaneously
<?php $staff = array(1,2,3,4,5,6,7,8,9,10); $start = array("11:00", "13:00", "17:00", "17:00", "11:00", "13:30", "16:50", "18:30","17:00", "11:00"); $end = array("21:00", "15:00", "19:00", "19:30", "11:30", "15:10", "18:45", "19:45", "19:00", "11:30"); // Add staff number to end of time ex 11:00 => 11:00#2 For($i=0; $i<count($start);$i++){ $start[$i] .= "#" . $staff[$i]; $end[$i] .= "#" . $staff[$i]; } $t = array_merge($start,$end); // create one long array with all in and out times sort($t); //var_dump($t); // Multisport is needed to get all arrays in time order as reference array_multisort($start, $end, $staff); // Find first start time (11:00) and slice array thwre, build string $test = implode(PHP_EOL,array_slice($t, array_search($start[0], $t))); // Find the times before first start (night end times) and add them last in string $test .= PHP_EOL . implode(PHP_EOL,array_slice($t, 0,array_search($start[0], $t))); $times = explode(PHP_EOL, $test); // explode to make it array again // Var_dump($times); $WhoIsInDaHouse = array("dummy"); // add a dummy variable since 0=false in later if $j=0; for($i=0; $i<count($times);$i++){ //echo $times[$i] ." " . $i ."\n"; if($times[$i]){ $TimePerson = explode("#", $times[$i]); $Time = $TimePerson[0]; $person = $TimePerson[1]; $inout = array_search($person, $WhoIsInDaHouse); //is person in house and about to leave? If($inout != false){ //if person enter work false, if true: key of person leaving in $WhoIsInDaHouse //Here $person is leaving work Unset($WhoIsInDaHouse[$inout]); If(count($WhoIsInDaHouse) == 2){ // someone will now be alone since we have a dummy $Alone[$j]["start"] = $Time; $Alone[$j]["who"] = array_slice($WhoIsInDaHouse, -1)[0]; }elseif(count($WhoIsInDaHouse) == 1 && $prevcount == 2){ // Only dummy left $Alone[$j]["end"] = $Time; $Alone[$j]["duration"] = strtotime($Alone[$j]["end"])-strtotime($Alone[$j]["start"]); $j++; } }Else{ // Here person enters work $WhoIsInDaHouse[] = $person; If(count($WhoIsInDaHouse) == 2){ // someone is entering alone $Alone[$j]["start"] = $Time; $Alone[$j]["who"] = $person; }elseif(count($WhoIsInDaHouse)>2 && $prevcount == 2){ // not alone anymore $Alone[$j]["end"] = $Time; $Alone[$j]["duration"] = strtotime($Alone[$j]["end"])-strtotime($Alone[$j]["start"]); $j++; } } $prevcount = count($WhoIsInDaHouse); } } foreach($Alone as $key => &$loner){ if($loner["duration"]==0) unset($Alone[$key]); } Var_dump($Alone);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 5
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 162
Branch analysis from position: 162
2 jumps found. (Code = 44) Position 1 = 165, Position 2 = 63
Branch analysis from position: 165
2 jumps found. (Code = 125) Position 1 = 166, Position 2 = 173
Branch analysis from position: 166
2 jumps found. (Code = 126) Position 1 = 167, Position 2 = 173
Branch analysis from position: 167
2 jumps found. (Code = 43) Position 1 = 171, Position 2 = 172
Branch analysis from position: 171
1 jumps found. (Code = 42) Position 1 = 166
Branch analysis from position: 166
Branch analysis from position: 172
Branch analysis from position: 173
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 173
Branch analysis from position: 63
2 jumps found. (Code = 43) Position 1 = 65, Position 2 = 161
Branch analysis from position: 65
2 jumps found. (Code = 43) Position 1 = 82, Position 2 = 123
Branch analysis from position: 82
2 jumps found. (Code = 43) Position 1 = 86, Position 2 = 98
Branch analysis from position: 86
1 jumps found. (Code = 42) Position 1 = 122
Branch analysis from position: 122
1 jumps found. (Code = 42) Position 1 = 159
Branch analysis from position: 159
2 jumps found. (Code = 44) Position 1 = 165, Position 2 = 63
Branch analysis from position: 165
Branch analysis from position: 63
Branch analysis from position: 98
2 jumps found. (Code = 46) Position 1 = 101, Position 2 = 103
Branch analysis from position: 101
2 jumps found. (Code = 43) Position 1 = 104, Position 2 = 122
Branch analysis from position: 104
1 jumps found. (Code = 42) Position 1 = 159
Branch analysis from position: 159
Branch analysis from position: 122
Branch analysis from position: 103
Branch analysis from position: 123
2 jumps found. (Code = 43) Position 1 = 128, Position 2 = 135
Branch analysis from position: 128
1 jumps found. (Code = 42) Position 1 = 159
Branch analysis from position: 159
Branch analysis from position: 135
2 jumps found. (Code = 46) Position 1 = 138, Position 2 = 140
Branch analysis from position: 138
2 jumps found. (Code = 43) Position 1 = 141, Position 2 = 159
Branch analysis from position: 141
2 jumps found. (Code = 44) Position 1 = 165, Position 2 = 63
Branch analysis from position: 165
Branch analysis from position: 63
Branch analysis from position: 159
Branch analysis from position: 140
Branch analysis from position: 161
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 5
Branch analysis from position: 17
Branch analysis from position: 5
filename:       /in/bT2bZ
function name:  (null)
number of ops:  178
compiled vars:  !0 = $staff, !1 = $start, !2 = $end, !3 = $i, !4 = $t, !5 = $test, !6 = $times, !7 = $WhoIsInDaHouse, !8 = $j, !9 = $TimePerson, !10 = $Time, !11 = $person, !12 = $inout, !13 = $Alone, !14 = $prevcount, !15 = $loner, !16 = $key
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                       !0, <array>
    3     1        ASSIGN                                                       !1, <array>
    4     2        ASSIGN                                                       !2, <array>
    7     3        ASSIGN                                                       !3, 0
          4      > JMP                                                          ->14
    8     5    >   FETCH_DIM_R                                          ~22     !0, !3
          6        CONCAT                                               ~23     '%23', ~22
          7        ASSIGN_DIM_OP                    .=               8          !1, !3
          8        OP_DATA                                                      ~23
    9     9        FETCH_DIM_R                                          ~25     !0, !3
         10        CONCAT                                               ~26     '%23', ~25
         11        ASSIGN_DIM_OP                    .=               8          !2, !3
         12        OP_DATA                                                      ~26
    7    13        PRE_INC                                                      !3
         14    >   COUNT                                                ~28     !1
         15        IS_SMALLER                                                   !3, ~28
         16      > JMPNZ                                                        ~29, ->5
   12    17    >   INIT_FCALL                                                   'array_merge'
         18        SEND_VAR                                                     !1
         19        SEND_VAR                                                     !2
         20        DO_ICALL                                             $30     
         21        ASSIGN                                                       !4, $30
   13    22        INIT_FCALL                                                   'sort'
         23        SEND_REF                                                     !4
         24        DO_ICALL                                                     
   16    25        INIT_FCALL                                                   'array_multisort'
         26        SEND_REF                                                     !1
         27        SEND_REF                                                     !2
         28        SEND_REF                                                     !0
         29        DO_ICALL                                                     
   19    30        INIT_FCALL                                                   'array_slice'
         31        SEND_VAR                                                     !4
         32        INIT_FCALL                                                   'array_search'
         33        FETCH_DIM_R                                          ~34     !1, 0
         34        SEND_VAL                                                     ~34
         35        SEND_VAR                                                     !4
         36        DO_ICALL                                             $35     
         37        SEND_VAR                                                     $35
         38        DO_ICALL                                             $36     
         39        FRAMELESS_ICALL_2                implode             ~37     '%0A', $36
         40        ASSIGN                                                       !5, ~37
   22    41        INIT_FCALL                                                   'array_slice'
         42        SEND_VAR                                                     !4
         43        SEND_VAL                                                     0
         44        INIT_FCALL                                                   'array_search'
         45        FETCH_DIM_R                                          ~39     !1, 0
         46        SEND_VAL                                                     ~39
         47        SEND_VAR                                                     !4
         48        DO_ICALL                                             $40     
         49        SEND_VAR                                                     $40
         50        DO_ICALL                                             $41     
         51        FRAMELESS_ICALL_2                implode             ~42     '%0A', $41
         52        CONCAT                                               ~43     '%0A', ~42
         53        ASSIGN_OP                                         8          !5, ~43
   23    54        INIT_FCALL                                                   'explode'
         55        SEND_VAL                                                     '%0A'
         56        SEND_VAR                                                     !5
         57        DO_ICALL                                             $45     
         58        ASSIGN                                                       !6, $45
   26    59        ASSIGN                                                       !7, <array>
   27    60        ASSIGN                                                       !8, 0
   28    61        ASSIGN                                                       !3, 0
         62      > JMP                                                          ->162
   30    63    >   FETCH_DIM_R                                          ~50     !6, !3
         64      > JMPZ                                                         ~50, ->161
   31    65    >   INIT_FCALL                                                   'explode'
         66        SEND_VAL                                                     '%23'
         67        FETCH_DIM_R                                          ~51     !6, !3
         68        SEND_VAL                                                     ~51
         69        DO_ICALL                                             $52     
         70        ASSIGN                                                       !9, $52
   32    71        FETCH_DIM_R                                          ~54     !9, 0
         72        ASSIGN                                                       !10, ~54
   33    73        FETCH_DIM_R                                          ~56     !9, 1
         74        ASSIGN                                                       !11, ~56
   36    75        INIT_FCALL                                                   'array_search'
         76        SEND_VAR                                                     !11
         77        SEND_VAR                                                     !7
         78        DO_ICALL                                             $58     
         79        ASSIGN                                                       !12, $58
   37    80        IS_NOT_EQUAL                                                 !12, <false>
         81      > JMPZ                                                         ~60, ->123
   39    82    >   UNSET_DIM                                                    !7, !12
   41    83        COUNT                                                ~61     !7
         84        IS_EQUAL                                                     ~61, 2
         85      > JMPZ                                                         ~62, ->98
   42    86    >   FETCH_DIM_W                                          $63     !13, !8
         87        ASSIGN_DIM                                                   $63, 'start'
         88        OP_DATA                                                      !10
   43    89        INIT_FCALL                                                   'array_slice'
         90        SEND_VAR                                                     !7
         91        SEND_VAL                                                     -1
         92        DO_ICALL                                             $67     
         93        FETCH_DIM_R                                          ~68     $67, 0
         94        FETCH_DIM_W                                          $65     !13, !8
         95        ASSIGN_DIM                                                   $65, 'who'
         96        OP_DATA                                                      ~68
   41    97      > JMP                                                          ->122
   44    98    >   COUNT                                                ~69     !7
         99        IS_EQUAL                                             ~70     ~69, 1
        100      > JMPZ_EX                                              ~70     ~70, ->103
        101    >   IS_EQUAL                                             ~71     !14, 2
        102        BOOL                                                 ~70     ~71
        103    > > JMPZ                                                         ~70, ->122
   46   104    >   FETCH_DIM_W                                          $72     !13, !8
        105        ASSIGN_DIM                                                   $72, 'end'
        106        OP_DATA                                                      !10
   47   107        INIT_FCALL                                                   'strtotime'
        108        FETCH_DIM_R                                          ~76     !13, !8
        109        FETCH_DIM_R                                          ~77     ~76, 'end'
        110        SEND_VAL                                                     ~77
        111        DO_ICALL                                             $78     
        112        INIT_FCALL                                                   'strtotime'
        113        FETCH_DIM_R                                          ~79     !13, !8
        114        FETCH_DIM_R                                          ~80     ~79, 'start'
        115        SEND_VAL                                                     ~80
        116        DO_ICALL                                             $81     
        117        SUB                                                  ~82     $78, $81
        118        FETCH_DIM_W                                          $74     !13, !8
        119        ASSIGN_DIM                                                   $74, 'duration'
        120        OP_DATA                                                      ~82
   48   121        PRE_INC                                                      !8
   37   122    > > JMP                                                          ->159
   52   123    >   ASSIGN_DIM                                                   !7
        124        OP_DATA                                                      !11
   54   125        COUNT                                                ~85     !7
        126        IS_EQUAL                                                     ~85, 2
        127      > JMPZ                                                         ~86, ->135
   55   128    >   FETCH_DIM_W                                          $87     !13, !8
        129        ASSIGN_DIM                                                   $87, 'start'
        130        OP_DATA                                                      !10
   56   131        FETCH_DIM_W                                          $89     !13, !8
        132        ASSIGN_DIM                                                   $89, 'who'
        133        OP_DATA                                                      !11
   54   134      > JMP                                                          ->159
   57   135    >   COUNT                                                ~91     !7
        136        IS_SMALLER                                           ~92     2, ~91
        137      > JMPZ_EX                                              ~92     ~92, ->140
        138    >   IS_EQUAL                                             ~93     !14, 2
        139        BOOL                                                 ~92     ~93
        140    > > JMPZ                                                         ~92, ->159
   58   141    >   FETCH_DIM_W                                          $94     !13, !8
        142        ASSIGN_DIM                                                   $94, 'end'
        143        OP_DATA                                                      !10
   59   144        INIT_FCALL                                                   'strtotime'
        145        FETCH_DIM_R                                          ~98     !13, !8
        146        FETCH_DIM_R                                          ~99     ~98, 'end'
        147        SEND_VAL                                                     ~99
        148        DO_ICALL                                             $100    
        149        INIT_FCALL                                                   'strtotime'
        150        FETCH_DIM_R                                          ~101    !13, !8
        151        FETCH_DIM_R                                          ~102    ~101, 'start'
        152        SEND_VAL                                                     ~102
        153        DO_ICALL                                             $103    
        154        SUB                                                  ~104    $100, $103
        155        FETCH_DIM_W                                          $96     !13, !8
        156        ASSIGN_DIM                                                   $96, 'duration'
        157        OP_DATA                                                      ~104
   60   158        PRE_INC                                                      !8
   63   159    >   COUNT                                                ~106    !7
        160        ASSIGN                                                       !14, ~106
   28   161    >   PRE_INC                                                      !3
        162    >   COUNT                                                ~109    !6
        163        IS_SMALLER                                                   !3, ~109
        164      > JMPNZ                                                        ~110, ->63
   66   165    > > FE_RESET_RW                                          $111    !13, ->173
        166    > > FE_FETCH_RW                                          ~112    $111, !15, ->173
        167    >   ASSIGN                                                       !16, ~112
   67   168        FETCH_DIM_R                                          ~114    !15, 'duration'
        169        IS_EQUAL                                                     ~114, 0
        170      > JMPZ                                                         ~115, ->172
        171    >   UNSET_DIM                                                    !13, !16
   66   172    > > JMP                                                          ->166
        173    >   FE_FREE                                                      $111
   69   174        INIT_FCALL                                                   'var_dump'
        175        SEND_VAR                                                     !13
        176        DO_ICALL                                                     
        177      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
166.74 ms | 2771 KiB | 21 Q