3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = [ ['pc', ['pc', null, null], 'id' => 1, 'pid' => 0], ['laptop', ['pc', 'laptop', null], 'id' => 2, 'pid' => 0], ['acc', ['pc', 'acc', null], 'id' => 3, 'pid' => 0], ['bags', ['pc', 'acc', 'bags'], 'id' => 4, 'pid' => 0], ['adapter', ['pc', 'acc', 'adapter'], 'id' => 5, 'pid' => 0], ['clothes', ['clothes', null, null], 'id' => 6, 'pid' => 0] ]; function fillParentIds(array $input): array { return array_values(array_reduce($input, static function ($entriesByPath, $entry) { $hierarchy = array_filter($entry[1]); $pathToParent = implode('/', array_slice($hierarchy, 0, -1)); $pathToEntry = implode('/', $hierarchy); $entry['pid'] = array_key_exists($pathToParent, $entriesByPath) ? $entriesByPath[$pathToParent]['id'] : $entry['id']; $entriesByPath[$pathToEntry] = $entry; return $entriesByPath; }, [])); } print_r(fillParentIds($input));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DeORi
function name:  (null)
number of ops:  8
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   26     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'fillparentids'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function fillparentids:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DeORi
function name:  fillParentIds
number of ops:  14
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   14     1        INIT_FCALL                                               'array_values'
          2        INIT_FCALL                                               'array_reduce'
          3        SEND_VAR                                                 !0
          4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FDeORi%3A14%240'
   23     5        SEND_VAL                                                 ~1
          6        SEND_VAL                                                 <array>
          7        DO_ICALL                                         $2      
          8        SEND_VAR                                                 $2
          9        DO_ICALL                                         $3      
         10        VERIFY_RETURN_TYPE                                       $3
         11      > RETURN                                                   $3
   24    12*       VERIFY_RETURN_TYPE                                       
         13*     > RETURN                                                   null

End of function fillparentids

Function %00%7Bclosure%7D%2Fin%2FDeORi%3A14%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 28
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DeORi
function name:  {closure}
number of ops:  36
compiled vars:  !0 = $entriesByPath, !1 = $entry, !2 = $hierarchy, !3 = $pathToParent, !4 = $pathToEntry
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   15     2        INIT_FCALL                                               'array_filter'
          3        FETCH_DIM_R                                      ~5      !1, 1
          4        SEND_VAL                                                 ~5
          5        DO_ICALL                                         $6      
          6        ASSIGN                                                   !2, $6
   16     7        INIT_FCALL                                               'implode'
          8        SEND_VAL                                                 '%2F'
          9        INIT_FCALL                                               'array_slice'
         10        SEND_VAR                                                 !2
         11        SEND_VAL                                                 0
         12        SEND_VAL                                                 -1
         13        DO_ICALL                                         $8      
         14        SEND_VAR                                                 $8
         15        DO_ICALL                                         $9      
         16        ASSIGN                                                   !3, $9
   17    17        INIT_FCALL                                               'implode'
         18        SEND_VAL                                                 '%2F'
         19        SEND_VAR                                                 !2
         20        DO_ICALL                                         $11     
         21        ASSIGN                                                   !4, $11
   19    22        ARRAY_KEY_EXISTS                                         !3, !0
         23      > JMPZ                                                     ~14, ->28
         24    >   FETCH_DIM_R                                      ~15     !0, !3
         25        FETCH_DIM_R                                      ~16     ~15, 'id'
         26        QM_ASSIGN                                        ~17     ~16
         27      > JMP                                                      ->30
         28    >   FETCH_DIM_R                                      ~18     !1, 'id'
         29        QM_ASSIGN                                        ~17     ~18
         30    >   ASSIGN_DIM                                               !1, 'pid'
         31        OP_DATA                                                  ~17
   21    32        ASSIGN_DIM                                               !0, !4
         33        OP_DATA                                                  !1
   22    34      > RETURN                                                   !0
   23    35*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FDeORi%3A14%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
129.88 ms | 1407 KiB | 26 Q