3v4l.org

run code in 300+ PHP versions simultaneously
<?php $resultset = [ ['id' => '1a', 'parent' => null], ['id' => '2a', 'parent' => null], ['id' => '3a', 'parent' => '1a'], ['id' => '4a', 'parent' => null], ['id' => '5a', 'parent' => '3a'], ['id' => '6a', 'parent' => '5a'], ['id' => '7a', 'parent' => null], ['id' => '8a', 'parent' => '3a'] ]; function findParent(&$array, $parent = 0, $children = []){ // make $array modifiable foreach ($array as $i => &$row) { // make $row modifiable if ($parent) { // if not zero if ($i == $parent) { // found parent unset($array[$i]['parent']); $row = $children; // append child to parent's children subarray } elseif (!empty($row)) { // go down rabbit hole looking for parent findParent($row, $parent, $children); // look deeper for parent while preserving the initial parent_id and row // $row = $row['children']; } // else continue; } elseif ($row['parent']) { // child requires adoption unset($array[$i]); // remove child from level because it will be store elsewhere and won't be its own parent (reduce iterations in next loop & avoid infinite recursion) findParent($array, $row['parent'], $row); // look for parent using parent_id while carrying the entire row as the childarray } // else continue; } return $array; // return the modified array } var_export(findParent($resultset));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8LBIR
function name:  (null)
number of ops:  8
compiled vars:  !0 = $resultset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   31     1        INIT_FCALL                                               'var_export'
          2        INIT_FCALL                                               'findparent'
          3        SEND_REF                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function findparent:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 4, Position 2 = 33
Branch analysis from position: 4
2 jumps found. (Code = 126) Position 1 = 5, Position 2 = 33
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 22
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 21
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 21
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 32
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 32
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
filename:       /in/8LBIR
function name:  findParent
number of ops:  36
compiled vars:  !0 = $array, !1 = $parent, !2 = $children, !3 = $row, !4 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
          2        RECV_INIT                                        !2      <array>
   15     3      > FE_RESET_RW                                      $5      !0, ->33
          4    > > FE_FETCH_RW                                      ~6      $5, !3, ->33
          5    >   ASSIGN                                                   !4, ~6
   16     6      > JMPZ                                                     !1, ->22
   17     7    >   IS_EQUAL                                                 !4, !1
          8      > JMPZ                                                     ~8, ->13
   18     9    >   FETCH_DIM_UNSET                                  $9      !0, !4
         10        UNSET_DIM                                                $9, 'parent'
   19    11        ASSIGN                                                   !3, !2
         12      > JMP                                                      ->21
   20    13    >   ISSET_ISEMPTY_CV                                 ~11     !3
         14        BOOL_NOT                                         ~12     ~11
         15      > JMPZ                                                     ~12, ->21
   21    16    >   INIT_FCALL_BY_NAME                                       'findParent'
         17        SEND_VAR_EX                                              !3
         18        SEND_VAR_EX                                              !1
         19        SEND_VAR_EX                                              !2
         20        DO_FCALL                                      0          
         21    > > JMP                                                      ->32
   24    22    >   FETCH_DIM_R                                      ~14     !3, 'parent'
         23      > JMPZ                                                     ~14, ->32
   25    24    >   UNSET_DIM                                                !0, !4
   26    25        INIT_FCALL_BY_NAME                                       'findParent'
         26        SEND_VAR_EX                                              !0
         27        CHECK_FUNC_ARG                                           
         28        FETCH_DIM_FUNC_ARG                               $15     !3, 'parent'
         29        SEND_FUNC_ARG                                            $15
         30        SEND_VAR_EX                                              !3
         31        DO_FCALL                                      0          
   15    32    > > JMP                                                      ->4
         33    >   FE_FREE                                                  $5
   29    34      > RETURN                                                   !0
   30    35*     > RETURN                                                   null

End of function findparent

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.24 ms | 1403 KiB | 16 Q