<?php
class CategoryTree
{
private $catTree = array();
public function addCategory($category, $parent)
{
if($parent == null){
echo count(array_column($this->catTree, 'parent'), $parent);
}
else{
}
$temp = array("category" => $category,"parent" => $parent);
array_push($this->catTree,$temp);
}
public function getChildren($parent)
{
$that = $this;
$keys = array_keys(array_column($this->catTree, 'parent'), $parent);
return array_map(function($k) use ($that){return $that->catTree[$k]['category'];}, $keys);
}
}
// For testing purposes (do not submit uncommented):
$c = new CategoryTree;
$c->addCategory('A', null);
$c->addCategory('B', 'A');
$c->addCategory('C', 'A');
echo implode(',', $c->getChildren('A'));
preferences:
40.65 ms | 402 KiB | 5 Q