3v4l.org

run code in 300+ PHP versions simultaneously
<?php $existingRanges = [ ['start_date' => '2023-11-22', 'end_date' => '2023-11-30', 'price' => 200], ['start_date' => '2023-12-10', 'end_date' => '2023-12-14', 'price' => 100], ['start_date' => '2023-12-15', 'end_date' => '2023-12-20', 'price' => 100], ['start_date' => '2023-12-21', 'end_date' => '2023-12-27', 'price' => 100], ['start_date' => '2024-01-05', 'end_date' => '2024-01-05', 'price' => 600], ['start_date' => '2024-01-06', 'end_date' => '2024-01-07', 'price' => 700], ['start_date' => '2024-01-08', 'end_date' => '2024-01-20', 'price' => 700], ['start_date' => '2024-01-21', 'end_date' => '2024-01-31', 'price' => 100], ]; function mergeDateRanges($p_arrDateranges) { $finalArray = []; $arrLastDR = null; foreach ($p_arrDateranges as $arrDR) { if ($arrLastDR === null) { $arrLastDR = $arrDR; continue; } $endDate = DateTime::createFromFormat('Y-m-d', $arrLastDR['end_date']); $sLastDateE_1 = $endDate->modify('+1 day')->format('Y-m-d'); if (strtotime($arrDR['start_date']) == strtotime($sLastDateE_1) && $arrDR['price'] == $arrLastDR['price']) { $arrLastDR['end_date'] = $arrDR['end_date']; }else{ array_push($finalArray, $arrLastDR); $arrLastDR = $arrDR; } } if($arrLastDR !== null){ array_push($finalArray, $arrLastDR); } return $finalArray; } print_r(mergeDateRanges($existingRanges));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TfZlr
function name:  (null)
number of ops:  8
compiled vars:  !0 = $existingRanges
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   42     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'mergedateranges'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function mergedateranges:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 46
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 46
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 9
2 jumps found. (Code = 46) Position 1 = 31, Position 2 = 35
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 40
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 35
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 53
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 53
Branch analysis from position: 46
filename:       /in/TfZlr
function name:  mergeDateRanges
number of ops:  55
compiled vars:  !0 = $p_arrDateranges, !1 = $finalArray, !2 = $arrLastDR, !3 = $arrDR, !4 = $endDate, !5 = $sLastDateE_1
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   15     1        ASSIGN                                                   !1, <array>
   16     2        ASSIGN                                                   !2, null
   17     3      > FE_RESET_R                                       $8      !0, ->46
          4    > > FE_FETCH_R                                               $8, !3, ->46
   18     5    >   TYPE_CHECK                                    2          !2
          6      > JMPZ                                                     ~9, ->9
   19     7    >   ASSIGN                                                   !2, !3
   20     8      > JMP                                                      ->4
   23     9    >   INIT_STATIC_METHOD_CALL                                  'DateTime', 'createFromFormat'
         10        SEND_VAL                                                 'Y-m-d'
         11        FETCH_DIM_R                                      ~11     !2, 'end_date'
         12        SEND_VAL                                                 ~11
         13        DO_FCALL                                      0  $12     
         14        ASSIGN                                                   !4, $12
   24    15        INIT_METHOD_CALL                                         !4, 'modify'
         16        SEND_VAL_EX                                              '%2B1+day'
         17        DO_FCALL                                      0  $14     
         18        INIT_METHOD_CALL                                         $14, 'format'
         19        SEND_VAL_EX                                              'Y-m-d'
         20        DO_FCALL                                      0  $15     
         21        ASSIGN                                                   !5, $15
   26    22        INIT_FCALL                                               'strtotime'
         23        FETCH_DIM_R                                      ~17     !3, 'start_date'
         24        SEND_VAL                                                 ~17
         25        DO_ICALL                                         $18     
         26        INIT_FCALL                                               'strtotime'
         27        SEND_VAR                                                 !5
         28        DO_ICALL                                         $19     
         29        IS_EQUAL                                         ~20     $18, $19
         30      > JMPZ_EX                                          ~20     ~20, ->35
         31    >   FETCH_DIM_R                                      ~21     !3, 'price'
         32        FETCH_DIM_R                                      ~22     !2, 'price'
         33        IS_EQUAL                                         ~23     ~21, ~22
         34        BOOL                                             ~20     ~23
         35    > > JMPZ                                                     ~20, ->40
   27    36    >   FETCH_DIM_R                                      ~25     !3, 'end_date'
         37        ASSIGN_DIM                                               !2, 'end_date'
         38        OP_DATA                                                  ~25
   26    39      > JMP                                                      ->45
   29    40    >   INIT_FCALL                                               'array_push'
         41        SEND_REF                                                 !1
         42        SEND_VAR                                                 !2
         43        DO_ICALL                                                 
   30    44        ASSIGN                                                   !2, !3
   17    45    > > JMP                                                      ->4
         46    >   FE_FREE                                                  $8
   35    47        TYPE_CHECK                                  1020          !2
         48      > JMPZ                                                     ~28, ->53
   36    49    >   INIT_FCALL                                               'array_push'
         50        SEND_REF                                                 !1
         51        SEND_VAR                                                 !2
         52        DO_ICALL                                                 
   39    53    > > RETURN                                                   !1
   40    54*     > RETURN                                                   null

End of function mergedateranges

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
215.58 ms | 1020 KiB | 17 Q