3v4l.org

run code in 300+ PHP versions simultaneously
<?php function splitAndMergeDateRanges(array $dateRanges): array { if (empty($dateRanges)) { return []; } // First, sort the date ranges by their start date usort($dateRanges, function($a, $b) { return strcmp($a['start'], $b['start']); }); $mergedRanges = []; $currentRange = $dateRanges[0]; foreach ($dateRanges as $range) { if ($range['start'] <= $currentRange['end']) { // If the current range overlaps with the new range, merge them $currentRange['end'] = max($currentRange['end'], $range['end']); } else { // If they don't overlap, add the current range to the merged list $mergedRanges[] = $currentRange; // and start a new range $currentRange = $range; } } // Don't forget to add the last range $mergedRanges[] = $currentRange; return $mergedRanges; } // Example input $dateRanges = [ ["start" => "2024/06/11", "end" => "2024/06/12"], ["start" => "2024/06/12", "end" => "2024/06/13"], ["start" => "2024/06/13", "end" => "2024/06/14"], ["start" => "2024/06/15", "end" => "2024/06/16"], ["start" => "2024/06/16", "end" => "2024/06/17"], ["start" => "2024/06/17", "end" => "2024/06/18"], ["start" => "2024/06/18", "end" => "2024/06/19"], ["start" => "2024/06/19", "end" => "2024/06/20"], ["start" => "2024/06/20", "end" => "2024/06/21"], ["start" => "2024/06/22", "end" => "2024/06/23"], ["start" => "2024/06/23", "end" => "2024/06/24"], ["start" => "2024/06/24", "end" => "2024/06/25"], ["start" => "2024/06/25", "end" => "2024/06/26"], ["start" => "2024/06/26", "end" => "2024/06/27"], ["start" => "2024/06/27", "end" => "2024/06/28"], ["start" => "2024/06/28", "end" => "2024/06/29"], ["start" => "2024/06/29", "end" => "2024/06/30"] ]; print_r(splitAndMergeDateRanges($dateRanges)); splitAndMergeDateRanges($dateRanges);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O1rPl
function name:  (null)
number of ops:  11
compiled vars:  !0 = $dateRanges
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   ASSIGN                                                   !0, <array>
   58     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'splitandmergedateranges'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
   59     7        INIT_FCALL                                               'splitandmergedateranges'
          8        SEND_VAR                                                 !0
          9        DO_FCALL                                      0          
         10      > RETURN                                                   1

Function splitandmergedateranges:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 31
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 31
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 27
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
filename:       /in/O1rPl
function name:  splitAndMergeDateRanges
number of ops:  38
compiled vars:  !0 = $dateRanges, !1 = $mergedRanges, !2 = $currentRange, !3 = $range
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        ISSET_ISEMPTY_CV                                         !0
          2      > JMPZ                                                     ~4, ->4
    6     3    > > RETURN                                                   <array>
   10     4    >   INIT_FCALL                                               'usort'
          5        SEND_REF                                                 !0
          6        DECLARE_LAMBDA_FUNCTION                          ~5      [0]
   12     7        SEND_VAL                                                 ~5
   10     8        DO_ICALL                                                 
   14     9        ASSIGN                                                   !1, <array>
   15    10        FETCH_DIM_R                                      ~8      !0, 0
         11        ASSIGN                                                   !2, ~8
   17    12      > FE_RESET_R                                       $10     !0, ->31
         13    > > FE_FETCH_R                                               $10, !3, ->31
   18    14    >   FETCH_DIM_R                                      ~11     !3, 'start'
         15        FETCH_DIM_R                                      ~12     !2, 'end'
         16        IS_SMALLER_OR_EQUAL                                      ~11, ~12
         17      > JMPZ                                                     ~13, ->27
   20    18    >   INIT_FCALL                                               'max'
         19        FETCH_DIM_R                                      ~15     !2, 'end'
         20        SEND_VAL                                                 ~15
         21        FETCH_DIM_R                                      ~16     !3, 'end'
         22        SEND_VAL                                                 ~16
         23        DO_ICALL                                         $17     
         24        ASSIGN_DIM                                               !2, 'end'
         25        OP_DATA                                                  $17
   18    26      > JMP                                                      ->30
   23    27    >   ASSIGN_DIM                                               !1
         28        OP_DATA                                                  !2
   25    29        ASSIGN                                                   !2, !3
   17    30    > > JMP                                                      ->13
         31    >   FE_FREE                                                  $10
   30    32        ASSIGN_DIM                                               !1
         33        OP_DATA                                                  !2
   32    34        VERIFY_RETURN_TYPE                                       !1
         35      > RETURN                                                   !1
   33    36*       VERIFY_RETURN_TYPE                                       
         37*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O1rPl
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   11     2        INIT_FCALL                                               'strcmp'
          3        FETCH_DIM_R                                      ~2      !0, 'start'
          4        SEND_VAL                                                 ~2
          5        FETCH_DIM_R                                      ~3      !1, 'start'
          6        SEND_VAL                                                 ~3
          7        DO_ICALL                                         $4      
          8      > RETURN                                                   $4
   12     9*     > RETURN                                                   null

End of Dynamic Function 0

End of function splitandmergedateranges

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144 ms | 1437 KiB | 19 Q