3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(-1); header('Content-Type: text/plain; charset="utf-8"'); class NodeIterator extends \RecursiveArrayIterator { /** * @inheritdoc */ public function hasChildren() { return $this->current()->hasChildren(); } /** * @inheritdoc */ public function getChildren() { return new NodeIterator($this->current()->getChildren()); } } class Node implements \JsonSerializable, \IteratorAggregate { /** * @var mixed */ private $data; /** * @var Node[] */ private $children = []; /** * Node constructor. * * @param mixed $data */ public function __construct($data) { $this->data = $data; } /** * @return mixed */ public function getData() { return $this->data; } /** * @return bool */ public function hasChildren() { return count($this->children) > 0; } /** * @return \RecursiveArrayIterator */ public function getChildren() { return new \RecursiveArrayIterator($this->children); } /** * @param Node $child * * @return Node */ public function addChild(self $child) { if (in_array($child, $this->children, true)) { throw new \InvalidArgumentException('duplicate'); } $this->children[] = $child; return $child; } /** * @inheritdoc */ public function jsonSerialize() { return [ 'data' => $this->data, 'children' => $this->children ]; } /** * @inheritdoc */ public function getIterator() { return new NodeIterator([$this]); } } $root = new Node('root'); $root->addChild(new Node('sub1'))->addChild(new Node('sub1.1')); $root->addChild(new Node('sub2'))->addChild(new Node('sub2.1'))->addChild(new Node('sub.2.1.1')); $root->addChild(new Node('sub3')); print_r(json_encode($root, JSON_PRETTY_PRINT)); echo "\n\n"; foreach ($it = new RecursiveIteratorIterator(new NodeIterator([$root]), RecursiveIteratorIterator::SELF_FIRST) as $node) { echo str_repeat('.', $it->getDepth()), $node->getData(), "\n"; } echo "\n\n"; foreach ($it = new RecursiveIteratorIterator($root, RecursiveIteratorIterator::SELF_FIRST) as $node) { echo str_repeat('.', $it->getDepth()), $node->getData(), "\n"; }
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Return type of NodeIterator::hasChildren() should either be compatible with RecursiveArrayIterator::hasChildren(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/D0GfX on line 11 Deprecated: Return type of NodeIterator::getChildren() should either be compatible with RecursiveArrayIterator::getChildren(): ?RecursiveArrayIterator, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/D0GfX on line 19 Warning: Cannot modify header information - headers already sent by (output started at /in/D0GfX:6) in /in/D0GfX on line 3 Deprecated: Return type of Node::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/D0GfX on line 91 Deprecated: Return type of Node::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/D0GfX on line 102 { "data": "root", "children": [ { "data": "sub1", "children": [ { "data": "sub1.1", "children": [] } ] }, { "data": "sub2", "children": [ { "data": "sub2.1", "children": [ { "data": "sub.2.1.1", "children": [] } ] } ] }, { "data": "sub3", "children": [] } ] } root .sub1 ..sub1.1 .sub2 ..sub2.1 ...sub.2.1.1 .sub3 root .sub1 ..sub1.1 .sub2 ..sub2.1 ...sub.2.1.1 .sub3

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:
119.5 ms | 411 KiB | 5 Q