3v4l.org

run code in 500+ 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                              ~1      [0]
   23     5        SEND_VAL                                                     ~1
          6        SEND_VAL                                                     <array>
   14     7        DO_ICALL                                             $2      
   23     8        SEND_VAR                                                     $2
   14     9        DO_ICALL                                             $3      
   23    10        VERIFY_RETURN_TYPE                                           $3
         11      > RETURN                                                       $3
   24    12*       VERIFY_RETURN_TYPE                                           
         13*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 22
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DeORi
function name:  {closure:fillParentIds():14}
number of ops:  30
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                                                   'array_slice'
          8        SEND_VAR                                                     !2
          9        SEND_VAL                                                     0
         10        SEND_VAL                                                     -1
         11        DO_ICALL                                             $8      
         12        FRAMELESS_ICALL_2                implode             ~9      '%2F', $8
         13        ASSIGN                                                       !3, ~9
   17    14        FRAMELESS_ICALL_2                implode             ~11     '%2F', !2
         15        ASSIGN                                                       !4, ~11
   19    16        ARRAY_KEY_EXISTS                                             !3, !0
         17      > JMPZ                                                         ~14, ->22
         18    >   FETCH_DIM_R                                          ~15     !0, !3
         19        FETCH_DIM_R                                          ~16     ~15, 'id'
         20        QM_ASSIGN                                            ~17     ~16
         21      > JMP                                                          ->24
         22    >   FETCH_DIM_R                                          ~18     !1, 'id'
         23        QM_ASSIGN                                            ~17     ~18
         24    >   ASSIGN_DIM                                                   !1, 'pid'
         25        OP_DATA                                                      ~17
   21    26        ASSIGN_DIM                                                   !0, !4
         27        OP_DATA                                                      !1
   22    28      > RETURN                                                       !0
   23    29*     > RETURN                                                       null

End of Dynamic Function 0

End of function fillparentids

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
144.45 ms | 2545 KiB | 21 Q