3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array( array('id' => 1, 'title' => 'a', 'parent' => 0), array('id' => 2, 'title' => 'b', 'parent' => 1), array('id' => 3, 'title' => 'c', 'parent' => 1), array('id' => 5, 'title' => 'e', 'parent' => 0), array('id' => 6, 'title' => 'f', 'parent' => 5), array('id' => 7, 'title' => 'g', 'parent' => 5), array('id' => 8, 'title' => 'h', 'parent' => 0), array('id' => 9, 'title' => 'k', 'parent' => 8), array('id' => 10, 'title' => 'l', 'parent' => 8), array('id' => 11, 'title' => 'm', 'parent' => 10), array('id' => 12, 'title' => 'n', 'parent' => 10), ); function lookForParent($array, $id, $title) { $return = array(); for($i = 0; $i <= count($array); $i++) { //Look for the parent on the second level if(isset($array[$i][$id])) { $return[$i][$id][] = $title; break; } else { if(!empty($array[$i])) { $return_val = lookForParent($array[$i], $id, $title); if(!empty($return_val)) { $return[$i][$id][] = $return_val; break; } } } } return $return; } $new = array(); foreach($arr as $val) { if($val['parent'] > 0) { if(isset($new[$val['parent']])) { //Look if the parent exist on the first level $new[$val['parent']][$val['id']][] = $val['title']; } else { for($i = 0; $i <= count($new); $i++) { $return = lookForParent($new[$i], $val['id'], $val['title']); if(!empty($return)) { $new = array_merge($new, $return); } } } } else { $new[$val['id']][] = $val['title']; } } var_dump($new);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 48
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 48
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 42
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 18
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 20
Branch analysis from position: 41
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 37
Branch analysis from position: 32
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 20
Branch analysis from position: 41
Branch analysis from position: 20
Branch analysis from position: 37
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
filename:       /in/krMGK
function name:  (null)
number of ops:  53
compiled vars:  !0 = $arr, !1 = $new, !2 = $val, !3 = $i, !4 = $return
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   37     1        ASSIGN                                                   !1, <array>
   38     2      > FE_RESET_R                                       $7      !0, ->48
          3    > > FE_FETCH_R                                               $7, !2, ->48
   39     4    >   FETCH_DIM_R                                      ~8      !2, 'parent'
          5        IS_SMALLER                                               0, ~8
          6      > JMPZ                                                     ~9, ->42
   40     7    >   FETCH_DIM_R                                      ~10     !2, 'parent'
          8        ISSET_ISEMPTY_DIM_OBJ                         0          !1, ~10
          9      > JMPZ                                                     ~11, ->18
   41    10    >   FETCH_DIM_R                                      ~12     !2, 'parent'
         11        FETCH_DIM_R                                      ~14     !2, 'id'
         12        FETCH_DIM_R                                      ~17     !2, 'title'
         13        FETCH_DIM_W                                      $13     !1, ~12
         14        FETCH_DIM_W                                      $15     $13, ~14
         15        ASSIGN_DIM                                               $15
         16        OP_DATA                                                  ~17
         17      > JMP                                                      ->41
   43    18    >   ASSIGN                                                   !3, 0
         19      > JMP                                                      ->38
   44    20    >   INIT_FCALL                                               'lookforparent'
         21        FETCH_DIM_R                                      ~19     !1, !3
         22        SEND_VAL                                                 ~19
         23        FETCH_DIM_R                                      ~20     !2, 'id'
         24        SEND_VAL                                                 ~20
         25        FETCH_DIM_R                                      ~21     !2, 'title'
         26        SEND_VAL                                                 ~21
         27        DO_FCALL                                      0  $22     
         28        ASSIGN                                                   !4, $22
   45    29        ISSET_ISEMPTY_CV                                 ~24     !4
         30        BOOL_NOT                                         ~25     ~24
         31      > JMPZ                                                     ~25, ->37
   46    32    >   INIT_FCALL                                               'array_merge'
         33        SEND_VAR                                                 !1
         34        SEND_VAR                                                 !4
         35        DO_ICALL                                         $26     
         36        ASSIGN                                                   !1, $26
   43    37    >   PRE_INC                                                  !3
         38    >   COUNT                                            ~29     !1
         39        IS_SMALLER_OR_EQUAL                                      !3, ~29
         40      > JMPNZ                                                    ~30, ->20
         41    > > JMP                                                      ->47
   51    42    >   FETCH_DIM_R                                      ~31     !2, 'id'
         43        FETCH_DIM_R                                      ~34     !2, 'title'
         44        FETCH_DIM_W                                      $32     !1, ~31
         45        ASSIGN_DIM                                               $32
         46        OP_DATA                                                  ~34
   38    47    > > JMP                                                      ->3
         48    >   FE_FREE                                                  $7
   55    49        INIT_FCALL                                               'var_dump'
         50        SEND_VAR                                                 !1
         51        DO_ICALL                                                 
         52      > RETURN                                                   1

Function lookforparent:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 6
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 15
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 34
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 34
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
Branch analysis from position: 34
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 6
Branch analysis from position: 38
Branch analysis from position: 6
Branch analysis from position: 34
filename:       /in/krMGK
function name:  lookForParent
number of ops:  40
compiled vars:  !0 = $array, !1 = $id, !2 = $title, !3 = $return, !4 = $i, !5 = $return_val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   18     3        ASSIGN                                                   !3, <array>
   19     4        ASSIGN                                                   !4, 0
          5      > JMP                                                      ->35
   20     6    >   FETCH_DIM_IS                                     ~8      !0, !4
          7        ISSET_ISEMPTY_DIM_OBJ                         0          ~8, !1
          8      > JMPZ                                                     ~9, ->15
   21     9    >   FETCH_DIM_W                                      $10     !3, !4
         10        FETCH_DIM_W                                      $11     $10, !1
         11        ASSIGN_DIM                                               $11
         12        OP_DATA                                                  !2
   22    13      > JMP                                                      ->38
         14*       JMP                                                      ->34
   24    15    >   ISSET_ISEMPTY_DIM_OBJ                         1  ~13     !0, !4
         16        BOOL_NOT                                         ~14     ~13
         17      > JMPZ                                                     ~14, ->34
   25    18    >   INIT_FCALL_BY_NAME                                       'lookForParent'
         19        CHECK_FUNC_ARG                                           
         20        FETCH_DIM_FUNC_ARG                               $15     !0, !4
         21        SEND_FUNC_ARG                                            $15
         22        SEND_VAR_EX                                              !1
         23        SEND_VAR_EX                                              !2
         24        DO_FCALL                                      0  $16     
         25        ASSIGN                                                   !5, $16
   26    26        ISSET_ISEMPTY_CV                                 ~18     !5
         27        BOOL_NOT                                         ~19     ~18
         28      > JMPZ                                                     ~19, ->34
   27    29    >   FETCH_DIM_W                                      $20     !3, !4
         30        FETCH_DIM_W                                      $21     $20, !1
         31        ASSIGN_DIM                                               $21
         32        OP_DATA                                                  !5
   28    33      > JMP                                                      ->38
   19    34    >   PRE_INC                                                  !4
         35    >   COUNT                                            ~24     !0
         36        IS_SMALLER_OR_EQUAL                                      !4, ~24
         37      > JMPNZ                                                    ~25, ->6
   34    38    > > RETURN                                                   !3
   35    39*     > RETURN                                                   null

End of function lookforparent

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.75 ms | 1407 KiB | 18 Q