3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '[ { "id": "X", "label": "X", "data": { "23": 1, "26": 2, "20": 38944000 }, "children": [ { "id": "Y", "label": "Y", "data": { "23": 303000, "26": 704000, "20": 2486000 }, "children": [ { "id": "Z", "label": "Z", "data": { "23": 0, "26": 0, "20": 2486000 }, "children": [], "level": 2, "type": "category" }, { "id": "A", "label": "A", "data": { "23": 303000, "26": 704000, "20": 0 }, "children": [], "level": 2, "type": "category" } ], "level": 1, "type": "company" }, { "id": "B", "label": "B", "data": { "23": 9627000, "26": 9580000, "20": 9428000 }, "children": [ { "id": "C", "label": "C", "data": { "23": 6021000, "26": 6030000, "20": 9428000 }, "children": [], "level": 2, "type": "category" }, { "id": "D", "label": "D", "data": { "23": 2205000, "26": 1932000, "20": 0 }, "children": [], "level": 2, "type": "category" }, { "id": "E", "label": "E", "data": { "23": 737000, "26": 874000, "20": 0 }, "children": [], "level": 2, "type": "category" }, { "id": "F", "label": "F", "data": { "23": 664000, "26": 744000, "20": 0 }, "children": [], "level": 2, "type": "category" } ], "level": 1, "type": "company" } ], "level": 0, "type": "client" } ]'; function flatten($children) { $result = []; foreach($children as $child) { if (count($child->children) == 0) { $result[] = [ $child->type => $child->id, "23" => $child->data->{23}, "26" => $child->data->{26}, "20" => $child->data->{20}, ]; } else { foreach(flatten($child->children) as $deeper) { $result[] = [$child->type => $child->id] + $deeper; } } } return $result; } $tree = json_decode($json); $result = flatten($tree); print_r($result);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZFGNL
function name:  (null)
number of ops:  13
compiled vars:  !0 = $json, !1 = $tree, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%5B%0A++%7B%0A++++%22id%22%3A+%22X%22%2C%0A++++%22label%22%3A+%22X%22%2C%0A++++%22data%22%3A+%7B%0A++++++%2223%22%3A+1%2C%0A++++++%2226%22%3A+2%2C%0A++++++%2220%22%3A+38944000%0A++++%7D%2C%0A++++%22children%22%3A+%5B%0A++++++%7B%0A++++++++%22id%22%3A+%22Y%22%2C%0A++++++++%22label%22%3A+%22Y%22%2C%0A++++++++%22data%22%3A+%7B%0A++++++++++%2223%22%3A+303000%2C%0A++++++++++%2226%22%3A+704000%2C%0A++++++++++%2220%22%3A+2486000%0A++++++++%7D%2C%0A++++++++%22children%22%3A+%5B%0A++++++++++%7B%0A++++++++++++%22id%22%3A+%22Z%22%2C%0A++++++++++++%22label%22%3A+%22Z%22%2C%0A++++++++++++%22data%22%3A+%7B%0A++++++++++++++%2223%22%3A+0%2C%0A++++++++++++++%2226%22%3A+0%2C%0A++++++++++++++%2220%22%3A+2486000%0A++++++++++++%7D%2C%0A++++++++++++%22children%22%3A+%5B%5D%2C%0A++++++++++++%22level%22%3A+2%2C%0A++++++++++++%22type%22%3A+%22category%22%0A++++++++++%7D%2C%0A++++++++++%7B%0A++++++++++++%22id%22%3A+%22A%22%2C%0A++++++++++++%22label%22%3A+%22A%22%2C%0A++++++++++++%22data%22%3A+%7B%0A++++++++++++++%2223%22%3A+303000%2C%0A++++++++++++++%2226%22%3A+704000%2C%0A++++++++++++++%2220%22%3A+0%0A++++++++++++%7D%2C%0A++++++++++++%22children%22%3A+%5B%5D%2C%0A++++++++++++%22level%22%3A+2%2C%0A++++++++++++%22type%22%3A+%22category%22%0A++++++++++%7D%0A++++++++%5D%2C%0A++++++++%22level%22%3A+1%2C%0A++++++++%22type%22%3A+%22company%22%0A++++++%7D%2C%0A++++++%7B%0A++++++++%22id%22%3A+%22B%22%2C%0A++++++++%22label%22%3A+%22B%22%2C%0A++++++++%22data%22%3A+%7B%0A++++++++++%2223%22%3A+9627000%2C%0A++++++++++%2226%22%3A+9580000%2C%0A++++++++++%2220%22%3A+9428000%0A++++++++%7D%2C%0A++++++++%22children%22%3A+%5B%0A++++++++++%7B%0A++++++++++++%22id%22%3A+%22C%22%2C%0A++++++++++++%22label%22%3A+%22C%22%2C%0A++++++++++++%22data%22%3A+%7B%0A++++++++++++++%2223%22%3A+6021000%2C%0A++++++++++++++%2226%22%3A+6030000%2C%0A++++++++++++++%2220%22%3A+9428000%0A++++++++++++%7D%2C%0A++++++++++++%22children%22%3A+%5B%5D%2C%0A++++++++++++%22level%22%3A+2%2C%0A++++++++++++%22type%22%3A+%22category%22%0A++++++++++%7D%2C%0A++++++++++%7B%0A++++++++++++%22id%22%3A+%22D%22%2C%0A++++++++++++%22label%22%3A+%22D%22%2C%0A++++++++++++%22data%22%3A+%7B%0A++++++++++++++%2223%22%3A+2205000%2C%0A++++++++++++++%2226%22%3A+1932000%2C%0A++++++++++++++%2220%22%3A+0%0A++++++++++++%7D%2C%0A++++++++++++%22children%22%3A+%5B%5D%2C%0A++++++++++++%22level%22%3A+2%2C%0A++++++++++++%22type%22%3A+%22category%22%0A++++++++++%7D%2C%0A++++++++++%7B%0A++++++++++++%22id%22%3A+%22E%22%2C%0A++++++++++++%22label%22%3A+%22E%22%2C%0A++++++++++++%22data%22%3A+%7B%0A++++++++++++++%2223%22%3A+737000%2C%0A++++++++++++++%2226%22%3A+874000%2C%0A++++++++++++++%2220%22%3A+0%0A++++++++++++%7D%2C%0A++++++++++++%22children%22%3A+%5B%5D%2C%0A++++++++++++%22level%22%3A+2%2C%0A++++++++++++%22type%22%3A+%22category%22%0A++++++++++%7D%2C%0A++++++++++%7B%0A++++++++++++%22id%22%3A+%22F%22%2C%0A++++++++++++%22label%22%3A+%22F%22%2C%0A++++++++++++%22data%22%3A+%7B%0A++++++++++++++%2223%22%3A+664000%2C%0A++++++++++++++%2226%22%3A+744000%2C%0A++++++++++++++%2220%22%3A+0%0A++++++++++++%7D%2C%0A++++++++++++%22children%22%3A+%5B%5D%2C%0A++++++++++++%22level%22%3A+2%2C%0A++++++++++++%22type%22%3A+%22category%22%0A++++++++++%7D%0A++++++++%5D%2C%0A++++++++%22level%22%3A+1%2C%0A++++++++%22type%22%3A+%22company%22%0A++++++%7D%0A++++%5D%2C%0A++++%22level%22%3A+0%2C%0A++++%22type%22%3A+%22client%22%0A++%7D%0A%5D'
  136     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $4      
          4        ASSIGN                                                   !1, $4
  137     5        INIT_FCALL                                               'flatten'
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0  $6      
          8        ASSIGN                                                   !2, $6
  138     9        INIT_FCALL                                               'print_r'
         10        SEND_VAR                                                 !2
         11        DO_ICALL                                                 
         12      > RETURN                                                   1

Function flatten:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 39
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 39
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 23
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 23
2 jumps found. (Code = 77) Position 1 = 29, Position 2 = 37
Branch analysis from position: 29
2 jumps found. (Code = 78) Position 1 = 30, Position 2 = 37
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 37
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
filename:       /in/ZFGNL
function name:  flatten
number of ops:  42
compiled vars:  !0 = $children, !1 = $result, !2 = $child, !3 = $deeper
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  117     0  E >   RECV                                             !0      
  118     1        ASSIGN                                                   !1, <array>
  119     2      > FE_RESET_R                                       $5      !0, ->39
          3    > > FE_FETCH_R                                               $5, !2, ->39
  120     4    >   FETCH_OBJ_R                                      ~6      !2, 'children'
          5        COUNT                                            ~7      ~6
          6        IS_EQUAL                                                 ~7, 0
          7      > JMPZ                                                     ~8, ->23
  122     8    >   FETCH_OBJ_R                                      ~10     !2, 'type'
          9        FETCH_OBJ_R                                      ~11     !2, 'id'
         10        INIT_ARRAY                                       ~12     ~11, ~10
  123    11        FETCH_OBJ_R                                      ~13     !2, 'data'
         12        FETCH_OBJ_R                                      ~14     ~13, '23'
         13        ADD_ARRAY_ELEMENT                                ~12     ~14, 23
  124    14        FETCH_OBJ_R                                      ~15     !2, 'data'
         15        FETCH_OBJ_R                                      ~16     ~15, '26'
         16        ADD_ARRAY_ELEMENT                                ~12     ~16, 26
  125    17        FETCH_OBJ_R                                      ~17     !2, 'data'
         18        FETCH_OBJ_R                                      ~18     ~17, '20'
         19        ADD_ARRAY_ELEMENT                                ~12     ~18, 20
  121    20        ASSIGN_DIM                                               !1
  125    21        OP_DATA                                                  ~12
         22      > JMP                                                      ->38
  128    23    >   INIT_FCALL_BY_NAME                                       'flatten'
         24        CHECK_FUNC_ARG                                           
         25        FETCH_OBJ_FUNC_ARG                               $19     !2, 'children'
         26        SEND_FUNC_ARG                                            $19
         27        DO_FCALL                                      0  $20     
         28      > FE_RESET_R                                       $21     $20, ->37
         29    > > FE_FETCH_R                                               $21, !3, ->37
  129    30    >   FETCH_OBJ_R                                      ~23     !2, 'type'
         31        FETCH_OBJ_R                                      ~24     !2, 'id'
         32        INIT_ARRAY                                       ~25     ~24, ~23
         33        ADD                                              ~26     ~25, !3
         34        ASSIGN_DIM                                               !1
         35        OP_DATA                                                  ~26
  128    36      > JMP                                                      ->29
         37    >   FE_FREE                                                  $21
  119    38    > > JMP                                                      ->3
         39    >   FE_FREE                                                  $5
  133    40      > RETURN                                                   !1
  134    41*     > RETURN                                                   null

End of function flatten

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.99 ms | 1407 KiB | 18 Q