3v4l.org

run code in 500+ PHP versions simultaneously
<?php // The first group to assign un-prefixed items to $firstGroup = 'MAKE'; // Every possible word grouping $wordList = ['ENGINE', 'MODEL', 'POWER', 'TORQUE', 'GEAR', 'DRIVE', 'YEAR']; // Test string $string = 'Audi MODEL 80 ENGINE 1.9 TDi POWER 90Hk TORQUE 202Nm GEAR man DRIVE 2wd YEAR 1996'; // Key/value of group name and values $groups = []; // Default to the first group $currentWord = $firstGroup; foreach (explode(' ', $string) as $word) { // Found a special word, reset and continue the hunt if (in_array($word, $wordList)) { $currentWord = $word; continue; } // Assign. The subsequent for loop could be removed by just doing string concatenation here instead $groups[$currentWord][] = $word; } // Optional, join each back into a string foreach ($groups as $key => $values) { $groups[$key] = implode(' ', $values); } var_dump($groups);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 19
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 19
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 19
2 jumps found. (Code = 77) Position 1 = 21, Position 2 = 27
Branch analysis from position: 21
2 jumps found. (Code = 78) Position 1 = 22, Position 2 = 27
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
Branch analysis from position: 19
filename:       /in/D4pvl
function name:  (null)
number of ops:  32
compiled vars:  !0 = $firstGroup, !1 = $wordList, !2 = $string, !3 = $groups, !4 = $currentWord, !5 = $word, !6 = $values, !7 = $key
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                       !0, 'MAKE'
    7     1        ASSIGN                                                       !1, <array>
   10     2        ASSIGN                                                       !2, 'Audi+MODEL+80+ENGINE+1.9+TDi+POWER+90Hk+TORQUE+202Nm+GEAR+man+DRIVE+2wd+YEAR+1996'
   13     3        ASSIGN                                                       !3, <array>
   16     4        ASSIGN                                                       !4, !0
   17     5        INIT_FCALL                                                   'explode'
          6        SEND_VAL                                                     '+'
          7        SEND_VAR                                                     !2
          8        DO_ICALL                                             $13     
          9      > FE_RESET_R                                           $14     $13, ->19
         10    > > FE_FETCH_R                                                   $14, !5, ->19
   20    11    >   FRAMELESS_ICALL_2                in_array            ~15     !5, !1
         12      > JMPZ                                                         ~15, ->15
   21    13    >   ASSIGN                                                       !4, !5
   22    14      > JMP                                                          ->10
   26    15    >   FETCH_DIM_W                                          $17     !3, !4
         16        ASSIGN_DIM                                                   $17
         17        OP_DATA                                                      !5
   17    18      > JMP                                                          ->10
         19    >   FE_FREE                                                      $14
   30    20      > FE_RESET_R                                           $19     !3, ->27
         21    > > FE_FETCH_R                                           ~20     $19, !6, ->27
         22    >   ASSIGN                                                       !7, ~20
   31    23        FRAMELESS_ICALL_2                implode             ~23     '+', !6
         24        ASSIGN_DIM                                                   !3, !7
         25        OP_DATA                                                      ~23
   30    26      > JMP                                                          ->21
         27    >   FE_FREE                                                      $19
   34    28        INIT_FCALL                                                   'var_dump'
         29        SEND_VAR                                                     !3
         30        DO_ICALL                                                     
         31      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
165.42 ms | 1774 KiB | 15 Q