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 IteratorAggregate { 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]); } } foreach($trie as $node) var_dump(json_encode($node));
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
2 jumps found. (Code = 77) Position 1 = 44, Position 2 = 52
Branch analysis from position: 44
2 jumps found. (Code = 78) Position 1 = 45, Position 2 = 52
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
Branch analysis from position: 52
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 52
Branch analysis from position: 42
Branch analysis from position: 23
filename:       /in/FFnjN
function name:  (null)
number of ops:  54
compiled vars:  !0 = $list, !1 = $trie, !2 = $obj, !3 = $n, !4 = $p, !5 = $node
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                                         $6      
    2     3        ASSIGN                                                   !0, $6
   15     4        DECLARE_CLASS                                            'trie'
   46     5        DECLARE_CLASS                                            'node', 'trie'
   57     6        NEW                                              $8      'Trie'
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !1, $8
   59     9      > FE_RESET_R                                       $11     !0, ->23
         10    > > FE_FETCH_R                                       ~12     $11, !2, ->23
         11    >   ASSIGN                                                   !3, ~12
   60    12        FETCH_OBJ_R                                      ~14     !2, 'ParentTagId'
         13        BOOL_NOT                                         ~15     ~14
         14      > JMPZ                                                     ~15, ->22
   61    15    >   INIT_METHOD_CALL                                         !1, 'insert'
         16        NEW                                              $16     'Node'
         17        SEND_VAR_EX                                              !2
         18        DO_FCALL                                      0          
         19        SEND_VAR_NO_REF_EX                                       $16
         20        DO_FCALL                                      0          
   62    21        UNSET_DIM                                                !0, !3
   59    22    > > JMP                                                      ->10
         23    >   FE_FREE                                                  $11
   67    24      > FE_RESET_R                                       $19     !0, ->42
         25    > > FE_FETCH_R                                       ~20     $19, !2, ->42
         26    >   ASSIGN                                                   !3, ~20
   68    27        INIT_METHOD_CALL                                         !1, 'findById'
         28        CHECK_FUNC_ARG                                           
         29        FETCH_OBJ_FUNC_ARG                               $22     !2, 'ParentTagId'
         30        SEND_FUNC_ARG                                            $22
         31        DO_FCALL                                      0  $23     
         32        ASSIGN                                                   !4, $23
   69    33      > JMPZ                                                     !4, ->41
   70    34    >   INIT_METHOD_CALL                                         !4, 'insert'
         35        NEW                                              $25     'Node'
         36        SEND_VAR_EX                                              !2
         37        DO_FCALL                                      0          
         38        SEND_VAR_NO_REF_EX                                       $25
         39        DO_FCALL                                      0          
   71    40        UNSET_DIM                                                !0, !3
   67    41    > > JMP                                                      ->25
         42    >   FE_FREE                                                  $19
   76    43      > FE_RESET_R                                       $28     !1, ->52
         44    > > FE_FETCH_R                                               $28, !5, ->52
         45    >   INIT_FCALL                                               'var_dump'
         46        INIT_FCALL                                               'json_encode'
         47        SEND_VAR                                                 !5
         48        DO_ICALL                                         $29     
         49        SEND_VAR                                                 $29
         50        DO_ICALL                                                 
         51      > JMP                                                      ->44
         52    >   FE_FREE                                                  $28
         53      > RETURN                                                   1

Class Trie:
Function insert:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FFnjN
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/FFnjN
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/FFnjN
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/FFnjN
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/FFnjN
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:
177.08 ms | 1408 KiB | 19 Q