3v4l.org

run code in 500+ PHP versions simultaneously
<?php $array = [ [ 'name' => 'Event A', 'date' => '2023-02-31' ], [ 'name' => 'Event A', 'date' => '2023-03-01' ], [ 'name' => 'Event B', 'date' => '2023-03-02' ], [ 'name' => 'Event C', 'date' => '2023-03-03' ], [ 'name' => 'Event D', 'date' => '2023-04-07' ], [ 'name' => 'Event E', 'date' => '2023-04-08' ], [ 'name' => 'Event E', 'date' => '2023-04-09' ], [ 'name' => 'Event E', 'date' => '2023-04-19' ], [ 'name' => 'Event E', 'date' => '2023-04-20' ], [ 'name' => 'Event E', 'date' => '2023-04-24' ], ]; $result = []; $last = []; foreach ($array as ['date' => $date]) { sscanf($date, '%d-%d-%d', $y, $m, $d); $key = $y . $m; // group by year-month if (isset($last[$key])) { $lastPadded = sprintf('%02s', $last[$key]); $thisPadded = sprintf('%02s', $d); if (($d - 1) === $last[$key]) { $result[$key] = preg_replace("~-$lastPadded\b|\b$lastPadded\b\K~", "-$thisPadded", $result[$key]); // hyphenate } else { $result[$key] = preg_replace("~\b$lastPadded\b\K~", ", $thisPadded", $result[$key], 1); // comma-separate } } else { $result[$key] = sprintf('%02s / %02s / %d', $d, $m, $y); // push initial string for year-month } $last[$key] = $d; } echo implode(", ", $result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 73
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 73
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 62
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 47
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 73
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 73
filename:       /in/0S31d
function name:  (null)
number of ops:  77
compiled vars:  !0 = $array, !1 = $result, !2 = $last, !3 = $date, !4 = $y, !5 = $m, !6 = $d, !7 = $key, !8 = $lastPadded, !9 = $thisPadded
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   47     1        ASSIGN                                                       !1, <array>
   48     2        ASSIGN                                                       !2, <array>
   49     3      > FE_RESET_R                                           $13     !0, ->73
          4    > > FE_FETCH_R                                                   $13, $14, ->73
          5    >   FETCH_LIST_R                                         $15     $14, 'date'
          6        ASSIGN                                                       !3, $15
          7        FREE                                                         $14
   50     8        INIT_FCALL                                                   'sscanf'
          9        SEND_VAR                                                     !3
         10        SEND_VAL                                                     '%25d-%25d-%25d'
         11        SEND_REF                                                     !4
         12        SEND_REF                                                     !5
         13        SEND_REF                                                     !6
         14        DO_ICALL                                                     
   51    15        CONCAT                                               ~18     !4, !5
         16        ASSIGN                                                       !7, ~18
   52    17        ISSET_ISEMPTY_DIM_OBJ                             0          !2, !7
         18      > JMPZ                                                         ~20, ->62
   53    19    >   INIT_FCALL                                                   'sprintf'
         20        SEND_VAL                                                     '%2502s'
         21        FETCH_DIM_R                                          ~21     !2, !7
         22        SEND_VAL                                                     ~21
         23        DO_ICALL                                             $22     
         24        ASSIGN                                                       !8, $22
   54    25        INIT_FCALL                                                   'sprintf'
         26        SEND_VAL                                                     '%2502s'
         27        SEND_VAR                                                     !6
         28        DO_ICALL                                             $24     
         29        ASSIGN                                                       !9, $24
   55    30        SUB                                                  ~26     !6, 1
         31        FETCH_DIM_R                                          ~27     !2, !7
         32        IS_IDENTICAL                                                 ~26, ~27
         33      > JMPZ                                                         ~28, ->47
   56    34    >   ROPE_INIT                                         5  ~31     '%7E-'
         35        ROPE_ADD                                          1  ~31     ~31, !8
         36        ROPE_ADD                                          2  ~31     ~31, '%5Cb%7C%5Cb'
         37        ROPE_ADD                                          3  ~31     ~31, !8
         38        ROPE_END                                          4  ~30     ~31, '%5Cb%5CK%7E'
         39        NOP                                                          
         40        FAST_CONCAT                                          ~34     '-', !9
         41        FETCH_DIM_R                                          ~35     !1, !7
         42        FRAMELESS_ICALL_3                preg_replace        ~36     ~30, ~34
         43        OP_DATA                                                      ~35
         44        ASSIGN_DIM                                                   !1, !7
         45        OP_DATA                                                      ~36
   55    46      > JMP                                                          ->61
   58    47    >   INIT_FCALL                                                   'preg_replace'
         48        ROPE_INIT                                         3  ~39     '%7E%5Cb'
         49        ROPE_ADD                                          1  ~39     ~39, !8
         50        ROPE_END                                          2  ~38     ~39, '%5Cb%5CK%7E'
         51        SEND_VAL                                                     ~38
         52        NOP                                                          
         53        FAST_CONCAT                                          ~41     '%2C+', !9
         54        SEND_VAL                                                     ~41
         55        FETCH_DIM_R                                          ~42     !1, !7
         56        SEND_VAL                                                     ~42
         57        SEND_VAL                                                     1
         58        DO_ICALL                                             $43     
         59        ASSIGN_DIM                                                   !1, !7
         60        OP_DATA                                                      $43
   52    61    > > JMP                                                          ->70
   61    62    >   INIT_FCALL                                                   'sprintf'
         63        SEND_VAL                                                     '%2502s+%2F+%2502s+%2F+%25d'
         64        SEND_VAR                                                     !6
         65        SEND_VAR                                                     !5
         66        SEND_VAR                                                     !4
         67        DO_ICALL                                             $45     
         68        ASSIGN_DIM                                                   !1, !7
         69        OP_DATA                                                      $45
   63    70    >   ASSIGN_DIM                                                   !2, !7
         71        OP_DATA                                                      !6
   49    72      > JMP                                                          ->4
         73    >   FE_FREE                                                      $13
   65    74        FRAMELESS_ICALL_2                implode             ~47     '%2C+', !1
         75        ECHO                                                         ~47
         76      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
172.29 ms | 1776 KiB | 16 Q