3v4l.org

run code in 300+ 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 = 22
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 22
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 22
2 jumps found. (Code = 77) Position 1 = 24, Position 2 = 33
Branch analysis from position: 24
2 jumps found. (Code = 78) Position 1 = 25, Position 2 = 33
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
Branch analysis from position: 22
filename:       /in/D4pvl
function name:  (null)
number of ops:  38
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, ->22
         10    > > FE_FETCH_R                                               $14, !5, ->22
   20    11    >   INIT_FCALL                                               'in_array'
         12        SEND_VAR                                                 !5
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                         $15     
         15      > JMPZ                                                     $15, ->18
   21    16    >   ASSIGN                                                   !4, !5
   22    17      > JMP                                                      ->10
   26    18    >   FETCH_DIM_W                                      $17     !3, !4
         19        ASSIGN_DIM                                               $17
         20        OP_DATA                                                  !5
   17    21      > JMP                                                      ->10
         22    >   FE_FREE                                                  $14
   30    23      > FE_RESET_R                                       $19     !3, ->33
         24    > > FE_FETCH_R                                       ~20     $19, !6, ->33
         25    >   ASSIGN                                                   !7, ~20
   31    26        INIT_FCALL                                               'implode'
         27        SEND_VAL                                                 '+'
         28        SEND_VAR                                                 !6
         29        DO_ICALL                                         $23     
         30        ASSIGN_DIM                                               !3, !7
         31        OP_DATA                                                  $23
   30    32      > JMP                                                      ->24
         33    >   FE_FREE                                                  $19
   34    34        INIT_FCALL                                               'var_dump'
         35        SEND_VAR                                                 !3
         36        DO_ICALL                                                 
         37      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.31 ms | 1006 KiB | 17 Q