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

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

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/g07X5
function name:  __construct
number of ops:  9
compiled vars:  !0 = $obj, !1 = $v, !2 = $p
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
   40     1      > FE_RESET_R                                       $3      !0, ->7
          2    > > FE_FETCH_R                                       ~4      $3, !1, ->7
          3    >   ASSIGN                                                   !2, ~4
   41     4        ASSIGN_OBJ                                               !2
          5        OP_DATA                                                  !1
   40     6      > JMP                                                      ->2
          7    >   FE_FREE                                                  $3
   43     8      > RETURN                                                   null

End of function __construct

Function insert:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/g07X5
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/g07X5
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

End of class Node.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.1 ms | 1408 KiB | 17 Q