3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CategoryTree { private $categories = []; public function addCategory(string $category, string $parent = null): void { $this->categories[$parent][] = $category; } public function getChildren(string $parent): array { return $this->categories[$parent]; } } $c = new CategoryTree; $c->addCategory('A', null); $c->addCategory('B', 'A'); $c->addCategory('C', 'A'); echo implode(',', $c->getChildren('A')); //Result should be "B,C" or "C,B"

preferences:
16.14 ms | 402 KiB | 5 Q