3v4l.org

run code in 300+ 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 = 168
Branch analysis from position: 168
2 jumps found. (Code = 44) Position 1 = 171, Position 2 = 69
Branch analysis from position: 171
2 jumps found. (Code = 125) Position 1 = 172, Position 2 = 179
Branch analysis from position: 172
2 jumps found. (Code = 126) Position 1 = 173, Position 2 = 179
Branch analysis from position: 173
2 jumps found. (Code = 43) Position 1 = 177, Position 2 = 178
Branch analysis from position: 177
1 jumps found. (Code = 42) Position 1 = 172
Branch analysis from position: 172
Branch analysis from position: 178
Branch analysis from position: 179
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 179
Branch analysis from position: 69
2 jumps found. (Code = 43) Position 1 = 71, Position 2 = 167
Branch analysis from position: 71
2 jumps found. (Code = 43) Position 1 = 88, Position 2 = 129
Branch analysis from position: 88
2 jumps found. (Code = 43) Position 1 = 92, Position 2 = 104
Branch analysis from position: 92
1 jumps found. (Code = 42) Position 1 = 128
Branch analysis from position: 128
1 jumps found. (Code = 42) Position 1 = 165
Branch analysis from position: 165
2 jumps found. (Code = 44) Position 1 = 171, Position 2 = 69
Branch analysis from position: 171
Branch analysis from position: 69
Branch analysis from position: 104
2 jumps found. (Code = 46) Position 1 = 107, Position 2 = 109
Branch analysis from position: 107
2 jumps found. (Code = 43) Position 1 = 110, Position 2 = 128
Branch analysis from position: 110
1 jumps found. (Code = 42) Position 1 = 165
Branch analysis from position: 165
Branch analysis from position: 128
Branch analysis from position: 109
Branch analysis from position: 129
2 jumps found. (Code = 43) Position 1 = 134, Position 2 = 141
Branch analysis from position: 134
1 jumps found. (Code = 42) Position 1 = 165
Branch analysis from position: 165
Branch analysis from position: 141
2 jumps found. (Code = 46) Position 1 = 144, Position 2 = 146
Branch analysis from position: 144
2 jumps found. (Code = 43) Position 1 = 147, Position 2 = 165
Branch analysis from position: 147
2 jumps found. (Code = 44) Position 1 = 171, Position 2 = 69
Branch analysis from position: 171
Branch analysis from position: 69
Branch analysis from position: 165
Branch analysis from position: 146
Branch analysis from position: 167
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:  184
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                                               'implode'
         31        SEND_VAL                                                 '%0A'
         32        INIT_FCALL                                               'array_slice'
         33        SEND_VAR                                                 !4
         34        INIT_FCALL                                               'array_search'
         35        FETCH_DIM_R                                      ~34     !1, 0
         36        SEND_VAL                                                 ~34
         37        SEND_VAR                                                 !4
         38        DO_ICALL                                         $35     
         39        SEND_VAR                                                 $35
         40        DO_ICALL                                         $36     
         41        SEND_VAR                                                 $36
         42        DO_ICALL                                         $37     
         43        ASSIGN                                                   !5, $37
   22    44        INIT_FCALL                                               'implode'
         45        SEND_VAL                                                 '%0A'
         46        INIT_FCALL                                               'array_slice'
         47        SEND_VAR                                                 !4
         48        SEND_VAL                                                 0
         49        INIT_FCALL                                               'array_search'
         50        FETCH_DIM_R                                      ~39     !1, 0
         51        SEND_VAL                                                 ~39
         52        SEND_VAR                                                 !4
         53        DO_ICALL                                         $40     
         54        SEND_VAR                                                 $40
         55        DO_ICALL                                         $41     
         56        SEND_VAR                                                 $41
         57        DO_ICALL                                         $42     
         58        CONCAT                                           ~43     '%0A', $42
         59        ASSIGN_OP                                     8          !5, ~43
   23    60        INIT_FCALL                                               'explode'
         61        SEND_VAL                                                 '%0A'
         62        SEND_VAR                                                 !5
         63        DO_ICALL                                         $45     
         64        ASSIGN                                                   !6, $45
   26    65        ASSIGN                                                   !7, <array>
   27    66        ASSIGN                                                   !8, 0
   28    67        ASSIGN                                                   !3, 0
         68      > JMP                                                      ->168
   30    69    >   FETCH_DIM_R                                      ~50     !6, !3
         70      > JMPZ                                                     ~50, ->167
   31    71    >   INIT_FCALL                                               'explode'
         72        SEND_VAL                                                 '%23'
         73        FETCH_DIM_R                                      ~51     !6, !3
         74        SEND_VAL                                                 ~51
         75        DO_ICALL                                         $52     
         76        ASSIGN                                                   !9, $52
   32    77        FETCH_DIM_R                                      ~54     !9, 0
         78        ASSIGN                                                   !10, ~54
   33    79        FETCH_DIM_R                                      ~56     !9, 1
         80        ASSIGN                                                   !11, ~56
   36    81        INIT_FCALL                                               'array_search'
         82        SEND_VAR                                                 !11
         83        SEND_VAR                                                 !7
         84        DO_ICALL                                         $58     
         85        ASSIGN                                                   !12, $58
   37    86        BOOL                                             ~60     !12
         87      > JMPZ                                                     ~60, ->129
   39    88    >   UNSET_DIM                                                !7, !12
   41    89        COUNT                                            ~61     !7
         90        IS_EQUAL                                                 ~61, 2
         91      > JMPZ                                                     ~62, ->104
   42    92    >   FETCH_DIM_W                                      $63     !13, !8
         93        ASSIGN_DIM                                               $63, 'start'
         94        OP_DATA                                                  !10
   43    95        INIT_FCALL                                               'array_slice'
         96        SEND_VAR                                                 !7
         97        SEND_VAL                                                 -1
         98        DO_ICALL                                         $67     
         99        FETCH_DIM_R                                      ~68     $67, 0
        100        FETCH_DIM_W                                      $65     !13, !8
        101        ASSIGN_DIM                                               $65, 'who'
        102        OP_DATA                                                  ~68
   41   103      > JMP                                                      ->128
   44   104    >   COUNT                                            ~69     !7
        105        IS_EQUAL                                         ~70     ~69, 1
        106      > JMPZ_EX                                          ~70     ~70, ->109
        107    >   IS_EQUAL                                         ~71     !14, 2
        108        BOOL                                             ~70     ~71
        109    > > JMPZ                                                     ~70, ->128
   46   110    >   FETCH_DIM_W                                      $72     !13, !8
        111        ASSIGN_DIM                                               $72, 'end'
        112        OP_DATA                                                  !10
   47   113        INIT_FCALL                                               'strtotime'
        114        FETCH_DIM_R                                      ~76     !13, !8
        115        FETCH_DIM_R                                      ~77     ~76, 'end'
        116        SEND_VAL                                                 ~77
        117        DO_ICALL                                         $78     
        118        INIT_FCALL                                               'strtotime'
        119        FETCH_DIM_R                                      ~79     !13, !8
        120        FETCH_DIM_R                                      ~80     ~79, 'start'
        121        SEND_VAL                                                 ~80
        122        DO_ICALL                                         $81     
        123        SUB                                              ~82     $78, $81
        124        FETCH_DIM_W                                      $74     !13, !8
        125        ASSIGN_DIM                                               $74, 'duration'
        126        OP_DATA                                                  ~82
   48   127        PRE_INC                                                  !8
   37   128    > > JMP                                                      ->165
   52   129    >   ASSIGN_DIM                                               !7
        130        OP_DATA                                                  !11
   54   131        COUNT                                            ~85     !7
        132        IS_EQUAL                                                 ~85, 2
        133      > JMPZ                                                     ~86, ->141
   55   134    >   FETCH_DIM_W                                      $87     !13, !8
        135        ASSIGN_DIM                                               $87, 'start'
        136        OP_DATA                                                  !10
   56   137        FETCH_DIM_W                                      $89     !13, !8
        138        ASSIGN_DIM                                               $89, 'who'
        139        OP_DATA                                                  !11
   54   140      > JMP                                                      ->165
   57   141    >   COUNT                                            ~91     !7
        142        IS_SMALLER                                       ~92     2, ~91
        143      > JMPZ_EX                                          ~92     ~92, ->146
        144    >   IS_EQUAL                                         ~93     !14, 2
        145        BOOL                                             ~92     ~93
        146    > > JMPZ                                                     ~92, ->165
   58   147    >   FETCH_DIM_W                                      $94     !13, !8
        148        ASSIGN_DIM                                               $94, 'end'
        149        OP_DATA                                                  !10
   59   150        INIT_FCALL                                               'strtotime'
        151        FETCH_DIM_R                                      ~98     !13, !8
        152        FETCH_DIM_R                                      ~99     ~98, 'end'
        153        SEND_VAL                                                 ~99
        154        DO_ICALL                                         $100    
        155        INIT_FCALL                                               'strtotime'
        156        FETCH_DIM_R                                      ~101    !13, !8
        157        FETCH_DIM_R                                      ~102    ~101, 'start'
        158        SEND_VAL                                                 ~102
        159        DO_ICALL                                         $103    
        160        SUB                                              ~104    $100, $103
        161        FETCH_DIM_W                                      $96     !13, !8
        162        ASSIGN_DIM                                               $96, 'duration'
        163        OP_DATA                                                  ~104
   60   164        PRE_INC                                                  !8
   63   165    >   COUNT                                            ~106    !7
        166        ASSIGN                                                   !14, ~106
   28   167    >   PRE_INC                                                  !3
        168    >   COUNT                                            ~109    !6
        169        IS_SMALLER                                               !3, ~109
        170      > JMPNZ                                                    ~110, ->69
   66   171    > > FE_RESET_RW                                      $111    !13, ->179
        172    > > FE_FETCH_RW                                      ~112    $111, !15, ->179
        173    >   ASSIGN                                                   !16, ~112
   67   174        FETCH_DIM_R                                      ~114    !15, 'duration'
        175        IS_EQUAL                                                 ~114, 0
        176      > JMPZ                                                     ~115, ->178
        177    >   UNSET_DIM                                                !13, !16
   66   178    > > JMP                                                      ->172
        179    >   FE_FREE                                                  $111
   69   180        INIT_FCALL                                               'var_dump'
        181        SEND_VAR                                                 !13
        182        DO_ICALL                                                 
        183      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
228.23 ms | 1026 KiB | 22 Q