3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = [ "prefix1 foo", "prefix2 bar", "prefix1 aaa", "prefix2 bbb", "prefix3 ccc", "prefix1 111", "prefix2 222" ]; $prefixes = ['prefix1', 'prefix2', 'prefix3']; $output = array(); $lastidx = count($prefixes); foreach ($input as $item) { list($prefix, $value) = explode(' ', $item); $index = array_search($prefix, $prefixes); if ($index < $lastidx) $output[] = array(); $output[count($output)-1][$prefix] = $value; $lastidx = $index; } print_r($output);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 32
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 32
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 25
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 25
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/1fkMM
function name:  (null)
number of ops:  37
compiled vars:  !0 = $input, !1 = $prefixes, !2 = $output, !3 = $lastidx, !4 = $item, !5 = $prefix, !6 = $value, !7 = $index
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   12     1        ASSIGN                                                   !1, <array>
   13     2        ASSIGN                                                   !2, <array>
   14     3        COUNT                                            ~11     !1
          4        ASSIGN                                                   !3, ~11
   15     5      > FE_RESET_R                                       $13     !0, ->32
          6    > > FE_FETCH_R                                               $13, !4, ->32
   16     7    >   INIT_FCALL                                               'explode'
          8        SEND_VAL                                                 '+'
          9        SEND_VAR                                                 !4
         10        DO_ICALL                                         $14     
         11        FETCH_LIST_R                                     $15     $14, 0
         12        ASSIGN                                                   !5, $15
         13        FETCH_LIST_R                                     $17     $14, 1
         14        ASSIGN                                                   !6, $17
         15        FREE                                                     $14
   17    16        INIT_FCALL                                               'array_search'
         17        SEND_VAR                                                 !5
         18        SEND_VAR                                                 !1
         19        DO_ICALL                                         $19     
         20        ASSIGN                                                   !7, $19
   18    21        IS_SMALLER                                               !7, !3
         22      > JMPZ                                                     ~21, ->25
         23    >   ASSIGN_DIM                                               !2
         24        OP_DATA                                                  <array>
   19    25    >   COUNT                                            ~23     !2
         26        SUB                                              ~24     ~23, 1
         27        FETCH_DIM_W                                      $25     !2, ~24
         28        ASSIGN_DIM                                               $25, !5
         29        OP_DATA                                                  !6
   20    30        ASSIGN                                                   !3, !7
   15    31      > JMP                                                      ->6
         32    >   FE_FREE                                                  $13
   22    33        INIT_FCALL                                               'print_r'
         34        SEND_VAR                                                 !2
         35        DO_ICALL                                                 
         36      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
206.11 ms | 1401 KiB | 19 Q