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));
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Return type of Trie::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/FFnjN on line 40 Deprecated: Creation of dynamic property Node::$TagId is deprecated in /in/FFnjN on line 51 Deprecated: Creation of dynamic property Node::$ParentTagId is deprecated in /in/FFnjN on line 51 Deprecated: Creation of dynamic property Node::$Name is deprecated in /in/FFnjN on line 51 Deprecated: Creation of dynamic property Node::$TagId is deprecated in /in/FFnjN on line 51 Deprecated: Creation of dynamic property Node::$ParentTagId is deprecated in /in/FFnjN on line 51 Deprecated: Creation of dynamic property Node::$Name is deprecated in /in/FFnjN on line 51 Deprecated: Creation of dynamic property Node::$TagId is deprecated in /in/FFnjN on line 51 Deprecated: Creation of dynamic property Node::$ParentTagId is deprecated in /in/FFnjN on line 51 Deprecated: Creation of dynamic property Node::$Name is deprecated in /in/FFnjN on line 51 Deprecated: Creation of dynamic property Node::$TagId is deprecated in /in/FFnjN on line 51 Deprecated: Creation of dynamic property Node::$ParentTagId is deprecated in /in/FFnjN on line 51 Deprecated: Creation of dynamic property Node::$Name is deprecated in /in/FFnjN on line 51 Fatal error: Uncaught Exception: Objects returned by Trie::getIterator() must be traversable or implement interface Iterator in /in/FFnjN:76 Stack trace: #0 {main} thrown in /in/FFnjN on line 76
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
66.08 ms | 403 KiB | 8 Q