3v4l.org

run code in 500+ PHP versions simultaneously
<?php $users = [ ['first_name' => 'Bob', 'last_name' => 'Smith'], ['first_name' => 'Steve', 'last_name' => 'Little'], ['first_name' => 'Eric', 'last_name' => 'Fielder'], ['first_name' => 'Steve', 'last_name' => 'Richardson'], ['first_name' => 'Bob', 'last_name' => 'Sanders'], ['first_name' => 'Bob', 'last_name' => 'Sanders'], ['first_name' => 'Bob', 'last_name' => 'Smith'], ]; foreach ($users as $row) { $grouped[$row['first_name']][$row['last_name'][0] ?? ''][$row['last_name']][] = $row; } $result = []; foreach ($grouped as $firstName => $leadingLetterGroup) { $leadingLetterCount = count($leadingLetterGroup); foreach ($leadingLetterGroup as $leadingLetter => $lastNameGroup) { $lastNameCount = count($lastNameGroup); foreach ($lastNameGroup as $lastName => $rows) { if (count($rows) === 1) { if ($leadingLetterCount === 1) { $username = $firstName; } elseif ($lastNameCount === 1) { $username = "$firstName $leadingLetter."; } else { $username = "$firstName $lastName"; } $result[] = $rows[0] + ['username' => $username]; } else { foreach ($rows as $i => $row) { $username = sprintf("%s %s (%d)", $firstName, $lastName, $i + 1); $result[] = $row + ['username' => $username]; } } } } } var_export($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 15
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 15
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 15
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 78
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 78
Branch analysis from position: 19
2 jumps found. (Code = 77) Position 1 = 23, Position 2 = 76
Branch analysis from position: 23
2 jumps found. (Code = 78) Position 1 = 24, Position 2 = 76
Branch analysis from position: 24
2 jumps found. (Code = 77) Position 1 = 28, Position 2 = 74
Branch analysis from position: 28
2 jumps found. (Code = 78) Position 1 = 29, Position 2 = 74
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 55
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 37
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 37
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 45
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 55
2 jumps found. (Code = 77) Position 1 = 56, Position 2 = 72
Branch analysis from position: 56
2 jumps found. (Code = 78) Position 1 = 57, Position 2 = 72
Branch analysis from position: 57
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
Branch analysis from position: 72
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 72
Branch analysis from position: 74
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 74
Branch analysis from position: 76
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 76
Branch analysis from position: 78
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 78
Branch analysis from position: 15
filename:       /in/BEGKT
function name:  (null)
number of ops:  83
compiled vars:  !0 = $users, !1 = $row, !2 = $grouped, !3 = $result, !4 = $leadingLetterGroup, !5 = $firstName, !6 = $leadingLetterCount, !7 = $lastNameGroup, !8 = $leadingLetter, !9 = $lastNameCount, !10 = $rows, !11 = $lastName, !12 = $username, !13 = $i
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   13     1      > FE_RESET_R                                           $15     !0, ->15
          2    > > FE_FETCH_R                                                   $15, !1, ->15
   14     3    >   FETCH_DIM_R                                          ~16     !1, 'first_name'
          4        FETCH_DIM_IS                                         ~18     !1, 'last_name'
          5        FETCH_DIM_IS                                         ~19     ~18, 0
          6        COALESCE                                             ~20     ~19
          7        QM_ASSIGN                                            ~20     ''
          8        FETCH_DIM_R                                          ~22     !1, 'last_name'
          9        FETCH_DIM_W                                          $17     !2, ~16
         10        FETCH_DIM_W                                          $21     $17, ~20
         11        FETCH_DIM_W                                          $23     $21, ~22
         12        ASSIGN_DIM                                                   $23
         13        OP_DATA                                                      !1
   13    14      > JMP                                                          ->2
         15    >   FE_FREE                                                      $15
   17    16        ASSIGN                                                       !3, <array>
   18    17      > FE_RESET_R                                           $26     !2, ->78
         18    > > FE_FETCH_R                                           ~27     $26, !4, ->78
         19    >   ASSIGN                                                       !5, ~27
   19    20        COUNT                                                ~29     !4
         21        ASSIGN                                                       !6, ~29
   20    22      > FE_RESET_R                                           $31     !4, ->76
         23    > > FE_FETCH_R                                           ~32     $31, !7, ->76
         24    >   ASSIGN                                                       !8, ~32
   21    25        COUNT                                                ~34     !7
         26        ASSIGN                                                       !9, ~34
   22    27      > FE_RESET_R                                           $36     !7, ->74
         28    > > FE_FETCH_R                                           ~37     $36, !10, ->74
         29    >   ASSIGN                                                       !11, ~37
   23    30        COUNT                                                ~39     !10
         31        IS_IDENTICAL                                                 ~39, 1
         32      > JMPZ                                                         ~40, ->55
   24    33    >   IS_IDENTICAL                                                 !6, 1
         34      > JMPZ                                                         ~41, ->37
   25    35    >   ASSIGN                                                       !12, !5
   24    36      > JMP                                                          ->49
   26    37    >   IS_IDENTICAL                                                 !9, 1
         38      > JMPZ                                                         ~43, ->45
   27    39    >   ROPE_INIT                                         4  ~45     !5
         40        ROPE_ADD                                          1  ~45     ~45, '+'
         41        ROPE_ADD                                          2  ~45     ~45, !8
         42        ROPE_END                                          3  ~44     ~45, '.'
         43        ASSIGN                                                       !12, ~44
   26    44      > JMP                                                          ->49
   29    45    >   ROPE_INIT                                         3  ~49     !5
         46        ROPE_ADD                                          1  ~49     ~49, '+'
         47        ROPE_END                                          2  ~48     ~49, !11
         48        ASSIGN                                                       !12, ~48
   31    49    >   FETCH_DIM_R                                          ~53     !10, 0
         50        INIT_ARRAY                                           ~54     !12, 'username'
         51        ADD                                                  ~55     ~53, ~54
         52        ASSIGN_DIM                                                   !3
         53        OP_DATA                                                      ~55
   23    54      > JMP                                                          ->73
   33    55    > > FE_RESET_R                                           $56     !10, ->72
         56    > > FE_FETCH_R                                           ~57     $56, !1, ->72
         57    >   ASSIGN                                                       !13, ~57
   34    58        ADD                                                  ~59     !13, 1
         59        ROPE_INIT                                         6  ~62     !5
         60        ROPE_ADD                                          1  ~62     ~62, '+'
         61        ROPE_ADD                                          2  ~62     ~62, !11
         62        ROPE_ADD                                          3  ~62     ~62, '+%28'
         63        CAST                                              4  ~60     ~59
         64        ROPE_ADD                                          4  ~62     ~62, ~60
         65        ROPE_END                                          5  ~61     ~62, '%29'
         66        ASSIGN                                                       !12, ~61
   35    67        INIT_ARRAY                                           ~67     !12, 'username'
         68        ADD                                                  ~68     !1, ~67
         69        ASSIGN_DIM                                                   !3
         70        OP_DATA                                                      ~68
   33    71      > JMP                                                          ->56
         72    >   FE_FREE                                                      $56
   22    73    > > JMP                                                          ->28
         74    >   FE_FREE                                                      $36
   20    75      > JMP                                                          ->23
         76    >   FE_FREE                                                      $31
   18    77      > JMP                                                          ->18
         78    >   FE_FREE                                                      $26
   41    79        INIT_FCALL                                                   'var_export'
         80        SEND_VAR                                                     !3
         81        DO_ICALL                                                     
         82      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
221.24 ms | 2169 KiB | 14 Q