3v4l.org

run code in 300+ 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 = 76
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 76
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 65
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 50
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 64
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 76
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 76
filename:       /in/0S31d
function name:  (null)
number of ops:  83
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, ->76
          4    > > FE_FETCH_R                                               $13, $14, ->76
          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, ->65
   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, ->50
   56    34    >   INIT_FCALL                                               'preg_replace'
         35        ROPE_INIT                                     5  ~31     '%7E-'
         36        ROPE_ADD                                      1  ~31     ~31, !8
         37        ROPE_ADD                                      2  ~31     ~31, '%5Cb%7C%5Cb'
         38        ROPE_ADD                                      3  ~31     ~31, !8
         39        ROPE_END                                      4  ~30     ~31, '%5Cb%5CK%7E'
         40        SEND_VAL                                                 ~30
         41        NOP                                                      
         42        FAST_CONCAT                                      ~34     '-', !9
         43        SEND_VAL                                                 ~34
         44        FETCH_DIM_R                                      ~35     !1, !7
         45        SEND_VAL                                                 ~35
         46        DO_ICALL                                         $36     
         47        ASSIGN_DIM                                               !1, !7
         48        OP_DATA                                                  $36
   55    49      > JMP                                                      ->64
   58    50    >   INIT_FCALL                                               'preg_replace'
         51        ROPE_INIT                                     3  ~39     '%7E%5Cb'
         52        ROPE_ADD                                      1  ~39     ~39, !8
         53        ROPE_END                                      2  ~38     ~39, '%5Cb%5CK%7E'
         54        SEND_VAL                                                 ~38
         55        NOP                                                      
         56        FAST_CONCAT                                      ~41     '%2C+', !9
         57        SEND_VAL                                                 ~41
         58        FETCH_DIM_R                                      ~42     !1, !7
         59        SEND_VAL                                                 ~42
         60        SEND_VAL                                                 1
         61        DO_ICALL                                         $43     
         62        ASSIGN_DIM                                               !1, !7
         63        OP_DATA                                                  $43
   52    64    > > JMP                                                      ->73
   61    65    >   INIT_FCALL                                               'sprintf'
         66        SEND_VAL                                                 '%2502s+%2F+%2502s+%2F+%25d'
         67        SEND_VAR                                                 !6
         68        SEND_VAR                                                 !5
         69        SEND_VAR                                                 !4
         70        DO_ICALL                                         $45     
         71        ASSIGN_DIM                                               !1, !7
         72        OP_DATA                                                  $45
   63    73    >   ASSIGN_DIM                                               !2, !7
         74        OP_DATA                                                  !6
   49    75      > JMP                                                      ->4
         76    >   FE_FREE                                                  $13
   65    77        INIT_FCALL                                               'implode'
         78        SEND_VAL                                                 '%2C+'
         79        SEND_VAR                                                 !1
         80        DO_ICALL                                         $47     
         81        ECHO                                                     $47
         82      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.6 ms | 1014 KiB | 17 Q