3v4l.org

run code in 500+ PHP versions simultaneously
<?php $array = [ [ 'id' => 1, 'parentId' => "" ], [ 'id' => 2, 'parentId' => 1 ], [ 'id' => 3, 'parentId' => 2 ] ]; $entriesById = array_column($array, null, 'id'); $entriesWithAncestors = array_reduce( $entriesById, static function (array $result, array $entry) use ($entriesById): array { $result[$entry['id']] = $entry + ['ancestors' => []]; $parentId = $entry['parentId']; while (isset($result[$parentId])) { $result[$entry['id']]['ancestors'][] = $result[$parentId]; $parentId = $entriesById[$parentId]['parentId'] ?? null; } return $result; }, [] ); print_r($entriesWithAncestors);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/L4e6P
function name:  (null)
number of ops:  19
compiled vars:  !0 = $array, !1 = $entriesById, !2 = $entriesWithAncestors
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   18     1        INIT_FCALL                                                   'array_column'
          2        SEND_VAR                                                     !0
          3        SEND_VAL                                                     null
          4        SEND_VAL                                                     'id'
          5        DO_ICALL                                             $4      
          6        ASSIGN                                                       !1, $4
   20     7        INIT_FCALL                                                   'array_reduce'
   21     8        SEND_VAR                                                     !1
   23     9        DECLARE_LAMBDA_FUNCTION                              ~6      [0]
         10        BIND_LEXICAL                                                 ~6, !1
   31    11        SEND_VAL                                                     ~6
         12        SEND_VAL                                                     <array>
   20    13        DO_ICALL                                             $7      
         14        ASSIGN                                                       !2, $7
   34    15        INIT_FCALL                                                   'print_r'
         16        SEND_VAR                                                     !2
         17        DO_ICALL                                                     
         18      > RETURN                                                       1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 10
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 10
Branch analysis from position: 23
Branch analysis from position: 10
filename:       /in/L4e6P
function name:  {closure:/in/L4e6P:23}
number of ops:  27
compiled vars:  !0 = $result, !1 = $entry, !2 = $entriesById, !3 = $parentId
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   23     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        BIND_STATIC                                                  !2
   24     3        FETCH_DIM_R                                          ~4      !1, 'id'
          4        ADD                                                  ~6      !1, <array>
          5        ASSIGN_DIM                                                   !0, ~4
          6        OP_DATA                                                      ~6
   25     7        FETCH_DIM_R                                          ~7      !1, 'parentId'
          8        ASSIGN                                                       !3, ~7
   26     9      > JMP                                                          ->21
   27    10    >   FETCH_DIM_R                                          ~9      !1, 'id'
         11        FETCH_DIM_R                                          ~13     !0, !3
         12        FETCH_DIM_W                                          $10     !0, ~9
         13        FETCH_DIM_W                                          $11     $10, 'ancestors'
         14        ASSIGN_DIM                                                   $11
         15        OP_DATA                                                      ~13
   28    16        FETCH_DIM_IS                                         ~14     !2, !3
         17        FETCH_DIM_IS                                         ~15     ~14, 'parentId'
         18        COALESCE                                             ~16     ~15
         19        QM_ASSIGN                                            ~16     null
         20        ASSIGN                                                       !3, ~16
   26    21    >   ISSET_ISEMPTY_DIM_OBJ                             0          !0, !3
         22      > JMPNZ                                                        ~18, ->10
   30    23    >   VERIFY_RETURN_TYPE                                           !0
         24      > RETURN                                                       !0
   31    25*       VERIFY_RETURN_TYPE                                           
         26*     > RETURN                                                       null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
163.4 ms | 2514 KiB | 16 Q