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 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
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
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
{ "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
Output for 5.3.10
Parse error: syntax error, unexpected '[' in /in/D0GfX on line 36
Process exited with code 255.

preferences:
166.18 ms | 411 KiB | 5 Q