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);
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Deprecated: Creation of dynamic property Node::$TagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$ParentTagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$Name is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$TagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$ParentTagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$Name is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$TagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$ParentTagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$Name is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$TagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$ParentTagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$Name is deprecated in /in/g07X5 on line 41 object(Trie)#8 (2) { ["parent":protected]=> NULL ["children":protected]=> array(2) { [0]=> object(Node)#9 (5) { ["parent":protected]=> *RECURSION* ["children":protected]=> array(2) { [0]=> object(Node)#7 (5) { ["parent":protected]=> *RECURSION* ["children":protected]=> array(0) { } ["TagId"]=> int(5) ["ParentTagId"]=> int(2) ["Name"]=> string(6) "bottom" } [1]=> object(Node)#1 (5) { ["parent":protected]=> *RECURSION* ["children":protected]=> array(0) { } ["TagId"]=> int(4) ["ParentTagId"]=> int(2) ["Name"]=> string(3) "top" } } ["TagId"]=> int(2) ["ParentTagId"]=> NULL ["Name"]=> string(5) "women" } [1]=> object(Node)#10 (5) { ["parent":protected]=> *RECURSION* ["children":protected]=> array(0) { } ["TagId"]=> int(16) ["ParentTagId"]=> NULL ["Name"]=> string(3) "men" } } }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Deprecated: Creation of dynamic property Node::$TagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$ParentTagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$Name is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$TagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$ParentTagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$Name is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$TagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$ParentTagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$Name is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$TagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$ParentTagId is deprecated in /in/g07X5 on line 41 Deprecated: Creation of dynamic property Node::$Name is deprecated in /in/g07X5 on line 41 object(Trie)#8 (2) { ["parent":protected]=> NULL ["children":protected]=> array(2) { [0]=> object(Node)#9 (5) { ["parent":protected]=> *RECURSION* ["children":protected]=> array(2) { [0]=> object(Node)#7 (5) { ["parent":protected]=> *RECURSION* ["children":protected]=> array(0) { } ["TagId"]=> int(5) ["ParentTagId"]=> int(2) ["Name"]=> string(6) "bottom" } [1]=> object(Node)#1 (5) { ["parent":protected]=> *RECURSION* ["children":protected]=> array(0) { } ["TagId"]=> int(4) ["ParentTagId"]=> int(2) ["Name"]=> string(3) "top" } } ["TagId"]=> int(2) ["ParentTagId"]=> NULL ["Name"]=> string(5) "women" } [1]=> object(Node)#10 (5) { ["parent":protected]=> *RECURSION* ["children":protected]=> array(0) { } ["TagId"]=> int(16) ["ParentTagId"]=> NULL ["Name"]=> string(3) "men" } } }
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
object(Trie)#8 (2) { ["parent":protected]=> NULL ["children":protected]=> array(2) { [0]=> object(Node)#9 (5) { ["parent":protected]=> *RECURSION* ["children":protected]=> array(2) { [0]=> object(Node)#7 (5) { ["parent":protected]=> *RECURSION* ["children":protected]=> array(0) { } ["TagId"]=> int(5) ["ParentTagId"]=> int(2) ["Name"]=> string(6) "bottom" } [1]=> object(Node)#1 (5) { ["parent":protected]=> *RECURSION* ["children":protected]=> array(0) { } ["TagId"]=> int(4) ["ParentTagId"]=> int(2) ["Name"]=> string(3) "top" } } ["TagId"]=> int(2) ["ParentTagId"]=> NULL ["Name"]=> string(5) "women" } [1]=> object(Node)#10 (5) { ["parent":protected]=> *RECURSION* ["children":protected]=> array(0) { } ["TagId"]=> int(16) ["ParentTagId"]=> NULL ["Name"]=> string(3) "men" } } }

preferences:
141.69 ms | 406 KiB | 181 Q