3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Provides a flat list of all the nodes * Eases with modifying the tree inheritance by id */ class NodeList { public $length = 0; /** * @param mixed $index * @return Node|null */ public function item($index) { $tmp = (array) $this; $this->length = count($tmp) - 1; if (false === isset($this->$index)) { return null; } return $this->$index; } } /** * Establish and maintain the tree view of each node/child */ class Tree extends NodeList { /** * Requires id, parent from record set * @param null|array|object $recordSet */ public function __construct($recordSet = null) { if (null !== $recordSet) { $this->setChildren($recordSet); } } /** * @param array|object $recordSet */ private function setChildren($recordSet) { foreach ($recordSet as $record) { if (true === is_array($record)) { $record = (object) $record; } if (true === isset($record->id)) { $this->length++; $this->appendNode($record->id, $record); } } foreach ($this as $node) { if (false === $node instanceof Node) { continue; } if (false === empty($node->parent) && true === isset($this->{$node->parent})) { $children = &$this->{$node->parent}->children; $children->length++; $children->{$node->id} = &$this->{$node->id}; $this->item($node->id); } } } /** * @param string $id * @param null|array|object $data * @return mixed */ public function appendNode($id, $data = null) { $this->$id = new Node($data); return $this->item($id); } /** * @param string $id * @return \Node */ public function getNode($id) { $item = $this->item($id); if (true === empty($item)) { $this->appendNode($id, null); } return $item; } /** * @param string $id * @return \Node|null */ public function getParent($id) { if (null === $this->getNode($id)->parent) { return null; } return $this->getNode($this->getNode($id)->parent); } /** * @param string $id * @return int */ public function getDepth($id) { $i = 0; $item = $this->item($id); if (null !== $item && null !== $item->parent) { $i = $this->getDepth($item->parent) + 1; } $item->depth = $i; return $item->depth; } /** * @param string $id */ public function removeNode($id) { $this->removeChildren($id); if (null !== $this->item(id)) { $parent = false; if ($this->item($id)->parent) { $parent = $this->getParent($id); } $this->$id = null; if ($parent && $parent->children) { unset($parent->children->$id); $parent->children->length--; } } } } /** * Single Node * This is an overloaded class */ class Node { public $id; public $name; public $parent; public $depth; public $children; /** * @param array|object $data */ public function __construct($data) { $this->children = new NodeList(); if (null === $data) { return; } foreach ($data as $key => $value) { /* I escaped these values since they are redundant to this script */ switch ($key) { case 'children': case 'depth': case 'childrenCount': continue 2; } $this->$key = $value; } } /** * @return int */ public function countChildren() { return $this->children->length; } /** * @return \NodeList */ public function getChildren() { return $this->children; } public function removeChildren() { if (null !== $this->getChildren()) { foreach ($this->children as $child) { if (true === isset($child->children)) { $child->removeChildren(); } $child = null; } } $this->children = null; return $this; } } $recordSet = array( array( 'id' => 1, 'name' => 'somename1', 'parent' => null, 'childrenCount' => 0, 'children' => 0, 'foo' => 'foo', ), array( 'id' => 53, 'name' => 'somename2', 'parent' => 1, 'childrenCount' => 0, 'children' => 0, 'bar' => 'bar', ), array( 'id' => 921, 'name' => 'somename3', 'parent' => 53, 'childrenCount' => 0, 'children' => 0, 'baz' => 'baz', ), ); $TreeObject = new Tree($recordSet); echo 'Display the entire tree (This is a fully recursive tree that can be looked up at any point)' . PHP_EOL; print_r($TreeObject); echo PHP_EOL; echo 'Get the middle node id 53' . PHP_EOL; print_r($TreeObject->getNode('53')); echo PHP_EOL . PHP_EOL; echo 'Get the child count of node id 53: ' . $TreeObject->getNode('53')->countChildren(); echo PHP_EOL . PHP_EOL; echo 'Get the children of node 1: ' . PHP_EOL; print_r($TreeObject->getNode('1')->getChildren()); echo PHP_EOL; echo 'Get the depth of the tree node id 921: ' . $TreeObject->getDepth('921'); echo PHP_EOL . PHP_EOL; echo 'Get the depth of the tree node id 53: ' . $TreeObject->getDepth('53'); echo PHP_EOL . PHP_EOL; echo 'Remove the children of node id 53:' . PHP_EOL; print_r($TreeObject->getNode('53')->removeChildren()); echo PHP_EOL; echo 'Display the list of nodes after removing the children of node id 53:' . PHP_EOL; print_r($TreeObject);

Abusive script

This script was stopped while abusing our resources

Output for 8.2.0 - 8.2.23, 8.3.0 - 8.3.11
Deprecated: Creation of dynamic property Node::$foo is deprecated in /in/PgqlG on line 183 Deprecated: Creation of dynamic property Tree::$1 is deprecated in /in/PgqlG on line 80 Deprecated: Creation of dynamic property Node::$bar is deprecated in /in/PgqlG on line 183 Deprecated: Creation of dynamic property Tree::$53 is deprecated in /in/PgqlG on line 80 Deprecated: Creation of dynamic property Node::$baz is deprecated in /in/PgqlG on line 183 Deprecated: Creation of dynamic property Tree::$921 is deprecated in /in/PgqlG on line 80 Deprecated: Creation of dynamic property NodeList::$53 is deprecated in /in/PgqlG on line 67 Deprecated: Creation of dynamic property NodeList::$921 is deprecated in /in/PgqlG on line 67 Display the entire tree (This is a fully recursive tree that can be looked up at any point) Tree Object ( [length] => 3 [1] => Node Object ( [id] => 1 [name] => somename1 [parent] => [depth] => [children] => NodeList Object ( [length] => 1 [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => [children] => NodeList Object ( [length] => 1 [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) [bar] => bar ) ) [foo] => foo ) [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => [children] => NodeList Object ( [length] => 1 [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) [bar] => bar ) [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) Get the middle node id 53 Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => [children] => NodeList Object ( [length] => 1 [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) [bar] => bar ) Get the child count of node id 53: 1 Get the children of node 1: NodeList Object ( [length] => 1 [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => [children] => NodeList Object ( [length] => 1 [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) [bar] => bar ) ) Get the depth of the tree node id 921: 2 Get the depth of the tree node id 53: 1 Remove the children of node id 53: Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => 1 [children] => [bar] => bar ) Display the list of nodes after removing the children of node id 53: Tree Object ( [length] => 3 [1] => Node Object ( [id] => 1 [name] => somename1 [parent] => [depth] => 0 [children] => NodeList Object ( [length] => 1 [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => 1 [children] => [bar] => bar ) ) [foo] => foo ) [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => 1 [children] => [bar] => bar ) [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => 2 [children] => [baz] => baz ) )
Output for 5.0.2 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.29
Display the entire tree (This is a fully recursive tree that can be looked up at any point) Tree Object ( [length] => 3 [1] => Node Object ( [id] => 1 [name] => somename1 [parent] => [depth] => [children] => NodeList Object ( [length] => 1 [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => [children] => NodeList Object ( [length] => 1 [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) [bar] => bar ) ) [foo] => foo ) [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => [children] => NodeList Object ( [length] => 1 [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) [bar] => bar ) [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) Get the middle node id 53 Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => [children] => NodeList Object ( [length] => 1 [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) [bar] => bar ) Get the child count of node id 53: 1 Get the children of node 1: NodeList Object ( [length] => 1 [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => [children] => NodeList Object ( [length] => 1 [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) [bar] => bar ) ) Get the depth of the tree node id 921: 2 Get the depth of the tree node id 53: 1 Remove the children of node id 53: Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => 1 [children] => [bar] => bar ) Display the list of nodes after removing the children of node id 53: Tree Object ( [length] => 3 [1] => Node Object ( [id] => 1 [name] => somename1 [parent] => [depth] => 0 [children] => NodeList Object ( [length] => 1 [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => 1 [children] => [bar] => bar ) ) [foo] => foo ) [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => 1 [children] => [bar] => bar ) [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => 2 [children] => [baz] => baz ) )
Output for 5.0.0 - 5.0.1
Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 247 Display the entire tree (This is a fully recursive tree that can be looked up at any point)PHP_EOLTree Object ( [length] => 3 [1] => Node Object ( [id] => 1 [name] => somename1 [parent] => [depth] => [children] => NodeList Object ( [length] => 1 [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => [children] => NodeList Object ( [length] => 1 [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) [bar] => bar ) ) [foo] => foo ) [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => [children] => NodeList Object ( [length] => 1 [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) [bar] => bar ) [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 249 PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 251 Get the middle node id 53PHP_EOLNode Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => [children] => NodeList Object ( [length] => 1 [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) [bar] => bar ) Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 253 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 253 PHP_EOLPHP_EOLGet the child count of node id 53: 1 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 256 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 256 PHP_EOLPHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 258 Get the children of node 1: PHP_EOLNodeList Object ( [length] => 1 [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => [children] => NodeList Object ( [length] => 1 [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => [children] => NodeList Object ( [length] => 0 ) [baz] => baz ) ) [bar] => bar ) ) Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 260 PHP_EOLGet the depth of the tree node id 921: 2 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 263 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 263 PHP_EOLPHP_EOLGet the depth of the tree node id 53: 1 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 266 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 266 PHP_EOLPHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 268 Remove the children of node id 53:PHP_EOLNode Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => 1 [children] => [bar] => bar ) Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 270 PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/PgqlG on line 272 Display the list of nodes after removing the children of node id 53:PHP_EOLTree Object ( [length] => 3 [1] => Node Object ( [id] => 1 [name] => somename1 [parent] => [depth] => 0 [children] => NodeList Object ( [length] => 1 [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => 1 [children] => [bar] => bar ) ) [foo] => foo ) [53] => Node Object ( [id] => 53 [name] => somename2 [parent] => 1 [depth] => 1 [children] => [bar] => bar ) [921] => Node Object ( [id] => 921 [name] => somename3 [parent] => 53 [depth] => 2 [children] => [baz] => baz ) )
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/PgqlG on line 10
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/PgqlG on line 10
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/PgqlG on line 10
Process exited with code 255.

preferences:
172.57 ms | 436 KiB | 5 Q