3v4l.org

run code in 500+ PHP versions simultaneously
<?php class NotJsonParser { public function convertPartsToObject(array $lines): array { $result = []; $result[]['items'] = &$items; foreach ($lines as $line) { if ($line[0] === '-') { // or however you want to identify the separator unset($items); $result[]['items'] = &$items; } else { $items[] = array_combine(['label', 'value', 'color'], explode('|', $line, 3)); } } return $result; } } $string = <<<STRING Label 1|80|default Label 2|100|default --- Frontend|80|red Backend|20|red --- Another Item 1|20|blue Another Item 2|20|blue - And another Item|20|blue STRING; $object = new NotJsonParser(); var_export($object->convertPartsToObject(explode(PHP_EOL, $string)));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UYXfD
function name:  (null)
number of ops:  15
compiled vars:  !0 = $string, !1 = $object
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   21     0  E >   ASSIGN                                                       !0, 'Label+1%7C80%7Cdefault%0ALabel+2%7C100%7Cdefault%0A---%0AFrontend%7C80%7Cred%0ABackend%7C20%7Cred%0A---%0AAnother+Item+1%7C20%7Cblue%0AAnother+Item+2%7C20%7Cblue%0A-%0AAnd+another+Item%7C20%7Cblue'
   34     1        NEW                                                  $3      'NotJsonParser'
          2        DO_FCALL                                          0          
          3        ASSIGN                                                       !1, $3
   35     4        INIT_FCALL                                                   'var_export'
          5        INIT_METHOD_CALL                                             !1, 'convertPartsToObject'
          6        INIT_FCALL                                                   'explode'
          7        SEND_VAL                                                     '%0A'
          8        SEND_VAR                                                     !0
          9        DO_ICALL                                             $6      
         10        SEND_VAR_NO_REF_EX                                           $6
         11        DO_FCALL                                          0  $7      
         12        SEND_VAR                                                     $7
         13        DO_ICALL                                                     
         14      > RETURN                                                       1

Class NotJsonParser:
Function convertpartstoobject:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 27
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 27
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 15
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
filename:       /in/UYXfD
function name:  convertPartsToObject
number of ops:  32
compiled vars:  !0 = $lines, !1 = $result, !2 = $items, !3 = $line
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    5     0  E >   RECV                                                 !0      
    7     1        ASSIGN                                                       !1, <array>
    8     2        FETCH_DIM_W                                          $5      !1
          3        FETCH_DIM_W                                          $6      $5, 'items'
          4        ASSIGN_REF                                                   $6, !2
    9     5      > FE_RESET_R                                           $8      !0, ->27
          6    > > FE_FETCH_R                                                   $8, !3, ->27
   10     7    >   FETCH_DIM_R                                          ~9      !3, 0
          8        IS_IDENTICAL                                                 ~9, '-'
          9      > JMPZ                                                         ~10, ->15
   11    10    >   UNSET_CV                                                     !2
   12    11        FETCH_DIM_W                                          $11     !1
         12        FETCH_DIM_W                                          $12     $11, 'items'
         13        ASSIGN_REF                                                   $12, !2
   10    14      > JMP                                                          ->26
   14    15    >   INIT_FCALL                                                   'array_combine'
         16        SEND_VAL                                                     <array>
         17        INIT_FCALL                                                   'explode'
         18        SEND_VAL                                                     '%7C'
         19        SEND_VAR                                                     !3
         20        SEND_VAL                                                     3
         21        DO_ICALL                                             $15     
         22        SEND_VAR                                                     $15
         23        DO_ICALL                                             $16     
         24        ASSIGN_DIM                                                   !2
         25        OP_DATA                                                      $16
    9    26    > > JMP                                                          ->6
         27    >   FE_FREE                                                      $8
   17    28        VERIFY_RETURN_TYPE                                           !1
         29      > RETURN                                                       !1
   18    30*       VERIFY_RETURN_TYPE                                           
         31*     > RETURN                                                       null

End of function convertpartstoobject

End of class NotJsonParser.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
171.78 ms | 2248 KiB | 16 Q