3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = <<<STR Maths Maths|Algebre Maths|Algebre|Algebre 1 Maths|Algebre|Algebre 2 Maths|Algebre|Algebre 3 Maths|Analyse Maths|Analyse|Analyse 1 Maths|Analyse|Analyse 2 Maths|Probabilité physics STR; function cleanupLines(array $lines): array { foreach ($lines as $index => $line) { if ($index > 0 && strpos($line, $lines[$index - 1]) === 0) { unset($lines[$index - 1]); } } return $lines; } function lineToArray(array $lineParts): array { if (count($lineParts) > 1) { return [$lineParts[0] => lineToArray(array_slice($lineParts, 1))]; } return $lineParts; } $lines = preg_split('/\r?\n/', $input); $lines = cleanupLines($lines); $result = array_reduce($lines, static function (array $result, string $line): array { $lineParts = explode('|', $line); $arrayPart = lineToArray($lineParts); return array_merge_recursive($result, $arrayPart); }, []); print_r($result);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0F1IM
function name:  (null)
number of ops:  21
compiled vars:  !0 = $input, !1 = $lines, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, 'Maths%0AMaths%7CAlgebre%0AMaths%7CAlgebre%7CAlgebre+1%0AMaths%7CAlgebre%7CAlgebre+2%0AMaths%7CAlgebre%7CAlgebre+3%0AMaths%7CAnalyse%0AMaths%7CAnalyse%7CAnalyse+1%0AMaths%7CAnalyse%7CAnalyse+2%0AMaths%7CProbabilit%C3%A9%0Aphysics'
   33     1        INIT_FCALL                                               'preg_split'
          2        SEND_VAL                                                 '%2F%5Cr%3F%5Cn%2F'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $4      
          5        ASSIGN                                                   !1, $4
   34     6        INIT_FCALL                                               'cleanuplines'
          7        SEND_VAR                                                 !1
          8        DO_FCALL                                      0  $6      
          9        ASSIGN                                                   !1, $6
   36    10        INIT_FCALL                                               'array_reduce'
         11        SEND_VAR                                                 !1
         12        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F0F1IM%3A36%240'
   40    13        SEND_VAL                                                 ~8
         14        SEND_VAL                                                 <array>
         15        DO_ICALL                                         $9      
   36    16        ASSIGN                                                   !2, $9
   42    17        INIT_FCALL                                               'print_r'
         18        SEND_VAR                                                 !2
         19        DO_ICALL                                                 
         20      > RETURN                                                   1

Function cleanuplines:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 18
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 18
Branch analysis from position: 3
2 jumps found. (Code = 46) Position 1 = 6, Position 2 = 14
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 17
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 17
Branch analysis from position: 14
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/0F1IM
function name:  cleanupLines
number of ops:  23
compiled vars:  !0 = $lines, !1 = $line, !2 = $index
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   17     1      > FE_RESET_R                                       $3      !0, ->18
          2    > > FE_FETCH_R                                       ~4      $3, !1, ->18
          3    >   ASSIGN                                                   !2, ~4
   18     4        IS_SMALLER                                       ~6      0, !2
          5      > JMPZ_EX                                          ~6      ~6, ->14
          6    >   INIT_FCALL                                               'strpos'
          7        SEND_VAR                                                 !1
          8        SUB                                              ~7      !2, 1
          9        FETCH_DIM_R                                      ~8      !0, ~7
         10        SEND_VAL                                                 ~8
         11        DO_ICALL                                         $9      
         12        IS_IDENTICAL                                     ~10     $9, 0
         13        BOOL                                             ~6      ~10
         14    > > JMPZ                                                     ~6, ->17
   19    15    >   SUB                                              ~11     !2, 1
         16        UNSET_DIM                                                !0, ~11
   17    17    > > JMP                                                      ->2
         18    >   FE_FREE                                                  $3
   22    19        VERIFY_RETURN_TYPE                                       !0
         20      > RETURN                                                   !0
   23    21*       VERIFY_RETURN_TYPE                                       
         22*     > RETURN                                                   null

End of function cleanuplines

Function linetoarray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 15
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0F1IM
function name:  lineToArray
number of ops:  19
compiled vars:  !0 = $lineParts
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   27     1        COUNT                                            ~1      !0
          2        IS_SMALLER                                               1, ~1
          3      > JMPZ                                                     ~2, ->15
   28     4    >   FETCH_DIM_R                                      ~3      !0, 0
          5        INIT_FCALL_BY_NAME                                       'lineToArray'
          6        INIT_FCALL                                               'array_slice'
          7        SEND_VAR                                                 !0
          8        SEND_VAL                                                 1
          9        DO_ICALL                                         $4      
         10        SEND_VAR_NO_REF_EX                                       $4
         11        DO_FCALL                                      0  $5      
         12        INIT_ARRAY                                       ~6      $5, ~3
         13        VERIFY_RETURN_TYPE                                       ~6
         14      > RETURN                                                   ~6
   30    15    >   VERIFY_RETURN_TYPE                                       !0
         16      > RETURN                                                   !0
   31    17*       VERIFY_RETURN_TYPE                                       
         18*     > RETURN                                                   null

End of function linetoarray

Function %00%7Bclosure%7D%2Fin%2F0F1IM%3A36%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0F1IM
function name:  {closure}
number of ops:  19
compiled vars:  !0 = $result, !1 = $line, !2 = $lineParts, !3 = $arrayPart
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   37     2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '%7C'
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $4      
          6        ASSIGN                                                   !2, $4
   38     7        INIT_FCALL                                               'linetoarray'
          8        SEND_VAR                                                 !2
          9        DO_FCALL                                      0  $6      
         10        ASSIGN                                                   !3, $6
   39    11        INIT_FCALL                                               'array_merge_recursive'
         12        SEND_VAR                                                 !0
         13        SEND_VAR                                                 !3
         14        DO_ICALL                                         $8      
         15        VERIFY_RETURN_TYPE                                       $8
         16      > RETURN                                                   $8
   40    17*       VERIFY_RETURN_TYPE                                       
         18*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F0F1IM%3A36%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
129.79 ms | 1411 KiB | 29 Q