3v4l.org

run code in 500+ PHP versions simultaneously
<?php $arr[] = ["id" => 1, "name" => "Electronics", "parent_id" => 0]; $arr[] = ["id" => 2, "name" => "Mobile", "parent_id" => 1]; $arr[] = ["id" => 3, "name" => "Iphone", "parent_id" => 2]; $arr[] = ["id" => 4, "name" => "Men", "parent_id" => 0]; $arr[] = ["id" => 5, "name" => "Shirt", "parent_id" => 4]; function addToArr(&$arr, $data) { if ($data["parent_id"] == 0) return $arr[] = ["id" => $data["id"], "name" => $data["name"], "children"=> []]; foreach($arr as &$e) { if ($e["id"] == $data["parent_id"]) { $e["children"][] = ["id" => $data["id"], "name" => $data["name"], "children"=> []]; break; } addToArr($e["children"], $data); } } $res = []; foreach($arr as $e) addToArr($res, $e); print_r($res);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 18
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/dXOKa
function name:  (null)
number of ops:  23
compiled vars:  !0 = $arr, !1 = $res, !2 = $e
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN_DIM                                                   !0
          1        OP_DATA                                                      <array>
    4     2        ASSIGN_DIM                                                   !0
          3        OP_DATA                                                      <array>
    5     4        ASSIGN_DIM                                                   !0
          5        OP_DATA                                                      <array>
    6     6        ASSIGN_DIM                                                   !0
          7        OP_DATA                                                      <array>
    7     8        ASSIGN_DIM                                                   !0
          9        OP_DATA                                                      <array>
   21    10        ASSIGN                                                       !1, <array>
   22    11      > FE_RESET_R                                           $9      !0, ->18
         12    > > FE_FETCH_R                                                   $9, !2, ->18
   23    13    >   INIT_FCALL                                                   'addtoarr'
         14        SEND_REF                                                     !1
         15        SEND_VAR                                                     !2
         16        DO_FCALL                                          0          
   22    17      > JMP                                                          ->12
         18    >   FE_FREE                                                      $9
   25    19        INIT_FCALL                                                   'print_r'
         20        SEND_VAR                                                     !1
         21        DO_ICALL                                                     
         22      > RETURN                                                       1

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

End of function addtoarr

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
133.47 ms | 485 KiB | 9 Q