3v4l.org

run code in 300+ PHP versions simultaneously
<?php $list = json_decode(<<<'JSON' [ { "TagId": 2, "ParentTagId": null, "Name": "women" }, { "TagId": 5, "ParentTagId": 2, "Name": "bottom" }, { "TagId": 4, "ParentTagId": 2, "Name": "top" }, { "TagId": 7, "ParentTagId": 4, "Name": "shirt" }, { "TagId": 8, "ParentTagId": 4, "Name": "tshirt" }, { "TagId": 12, "ParentTagId": 7, "Name": "longsleeve" }, { "TagId": 16, "ParentTagId": null, "Name": "men" } ] JSON ); class Trie implements getIterator { protected $parent; protected $children = []; public function insert(Node $node) { $node->parent = $this; $this->children[] = $node; } public function findById($id) { foreach($this->children as $childNode) { if ($childNode->TagId === $id) { return $childNode; } } } public function hasChildren() { return (bool) count($this->children); } public function getIterator() { return $this->children; } } class Node extends Trie { public function __construct(stdClass $obj) { foreach($obj as $p => $v) { $this->$p = $v; } } } $trie = new Trie; /* Insert all of the parentless nodes */ foreach($list as $n => $obj) { if (!$obj->ParentTagId) { $trie->insert(new Node($obj)); unset($list[$n]); } } /* Insert all of the child nodes */ foreach($list as $n => $obj) { $p = $trie->findById($obj->ParentTagId); if ($p) { $p->insert(new Node($obj)); unset($list[$n]); } } var_dump(json_encode($trie));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 23
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 23
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 22
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 22
Branch analysis from position: 23
2 jumps found. (Code = 77) Position 1 = 25, Position 2 = 42
Branch analysis from position: 25
2 jumps found. (Code = 78) Position 1 = 26, Position 2 = 42
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 41
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 41
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
Branch analysis from position: 23
filename:       /in/ubuKX
function name:  (null)
number of ops:  50
compiled vars:  !0 = $list, !1 = $trie, !2 = $obj, !3 = $n, !4 = $p
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   INIT_FCALL                                               'json_decode'
    3     1        SEND_VAL                                                 '%5B+%0A++%7B+%22TagId%22%3A+2%2C+%22ParentTagId%22%3A+null%2C+%22Name%22%3A+%22women%22+%7D%2C%0A++%7B+%22TagId%22%3A+5%2C+%22ParentTagId%22%3A+2%2C+%22Name%22%3A+%22bottom%22+%7D%2C%0A++%7B+%22TagId%22%3A+4%2C+%22ParentTagId%22%3A+2%2C+%22Name%22%3A+%22top%22+%7D%2C%0A++%7B+%22TagId%22%3A+7%2C+%22ParentTagId%22%3A+4%2C+%22Name%22%3A+%22shirt%22+%7D%2C%0A++%7B+%22TagId%22%3A+8%2C+%22ParentTagId%22%3A+4%2C+%22Name%22%3A+%22tshirt%22+%7D%2C%0A++%7B+%22TagId%22%3A+12%2C+%22ParentTagId%22%3A+7%2C+%22Name%22%3A+%22longsleeve%22+%7D%2C%0A++%7B+%22TagId%22%3A+16%2C+%22ParentTagId%22%3A+null%2C+%22Name%22%3A+%22men%22+%7D%0A%5D'
          2        DO_ICALL                                         $5      
    2     3        ASSIGN                                                   !0, $5
   15     4        DECLARE_CLASS                                            'trie'
   46     5        DECLARE_CLASS                                            'node', 'trie'
   57     6        NEW                                              $7      'Trie'
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !1, $7
   59     9      > FE_RESET_R                                       $10     !0, ->23
         10    > > FE_FETCH_R                                       ~11     $10, !2, ->23
         11    >   ASSIGN                                                   !3, ~11
   60    12        FETCH_OBJ_R                                      ~13     !2, 'ParentTagId'
         13        BOOL_NOT                                         ~14     ~13
         14      > JMPZ                                                     ~14, ->22
   61    15    >   INIT_METHOD_CALL                                         !1, 'insert'
         16        NEW                                              $15     'Node'
         17        SEND_VAR_EX                                              !2
         18        DO_FCALL                                      0          
         19        SEND_VAR_NO_REF_EX                                       $15
         20        DO_FCALL                                      0          
   62    21        UNSET_DIM                                                !0, !3
   59    22    > > JMP                                                      ->10
         23    >   FE_FREE                                                  $10
   67    24      > FE_RESET_R                                       $18     !0, ->42
         25    > > FE_FETCH_R                                       ~19     $18, !2, ->42
         26    >   ASSIGN                                                   !3, ~19
   68    27        INIT_METHOD_CALL                                         !1, 'findById'
         28        CHECK_FUNC_ARG                                           
         29        FETCH_OBJ_FUNC_ARG                               $21     !2, 'ParentTagId'
         30        SEND_FUNC_ARG                                            $21
         31        DO_FCALL                                      0  $22     
         32        ASSIGN                                                   !4, $22
   69    33      > JMPZ                                                     !4, ->41
   70    34    >   INIT_METHOD_CALL                                         !4, 'insert'
         35        NEW                                              $24     'Node'
         36        SEND_VAR_EX                                              !2
         37        DO_FCALL                                      0          
         38        SEND_VAR_NO_REF_EX                                       $24
         39        DO_FCALL                                      0          
   71    40        UNSET_DIM                                                !0, !3
   67    41    > > JMP                                                      ->25
         42    >   FE_FREE                                                  $18
   76    43        INIT_FCALL                                               'var_dump'
         44        INIT_FCALL                                               'json_encode'
         45        SEND_VAR                                                 !1
         46        DO_ICALL                                         $27     
         47        SEND_VAR                                                 $27
         48        DO_ICALL                                                 
         49      > RETURN                                                   1

Class Trie:
Function insert:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ubuKX
function name:  insert
number of ops:  8
compiled vars:  !0 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
   22     1        FETCH_THIS                                       ~2      
          2        ASSIGN_OBJ                                               !0, 'parent'
          3        OP_DATA                                                  ~2
   23     4        FETCH_OBJ_W                                      $3      'children'
          5        ASSIGN_DIM                                               $3
          6        OP_DATA                                                  !0
   24     7      > RETURN                                                   null

End of function insert

Function findbyid:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 10
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/ubuKX
function name:  findById
number of ops:  12
compiled vars:  !0 = $id, !1 = $childNode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
   28     1        FETCH_OBJ_R                                      ~2      'children'
          2      > FE_RESET_R                                       $3      ~2, ->10
          3    > > FE_FETCH_R                                               $3, !1, ->10
   29     4    >   FETCH_OBJ_R                                      ~4      !1, 'TagId'
          5        IS_IDENTICAL                                             !0, ~4
          6      > JMPZ                                                     ~5, ->9
   30     7    >   FE_FREE                                                  $3
          8      > RETURN                                                   !1
   28     9    > > JMP                                                      ->3
         10    >   FE_FREE                                                  $3
   33    11      > RETURN                                                   null

End of function findbyid

Function haschildren:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ubuKX
function name:  hasChildren
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   FETCH_OBJ_R                                      ~0      'children'
          1        COUNT                                            ~1      ~0
          2        BOOL                                             ~2      ~1
          3      > RETURN                                                   ~2
   38     4*     > RETURN                                                   null

End of function haschildren

Function getiterator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ubuKX
function name:  getIterator
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   FETCH_OBJ_R                                      ~0      'children'
          1      > RETURN                                                   ~0
   43     2*     > RETURN                                                   null

End of function getiterator

End of class Trie.

Class Node:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 7
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/ubuKX
function name:  __construct
number of ops:  9
compiled vars:  !0 = $obj, !1 = $v, !2 = $p
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
   50     1      > FE_RESET_R                                       $3      !0, ->7
          2    > > FE_FETCH_R                                       ~4      $3, !1, ->7
          3    >   ASSIGN                                                   !2, ~4
   51     4        ASSIGN_OBJ                                               !2
          5        OP_DATA                                                  !1
   50     6      > JMP                                                      ->2
          7    >   FE_FREE                                                  $3
   53     8      > RETURN                                                   null

End of function __construct

End of class Node.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
168.07 ms | 1404 KiB | 19 Q