嗨,任何人都可以幫我解決這個問題,提前致謝
class CategoryTree
{
public function addCategory(string $category, string $parent=null) : void
{
}
public function getChildren(string $parent) : array
{
return [];
}
}
$c = new CategoryTree;
$c->addCategory('A', null);
$c->addCategory('B', 'A');
$c->addCategory('C', 'A');
echo implode(',', $c->getChildren('A'));
結果應該是“B,C”或“C,B”。
uj5u.com熱心網友回復:
您可以使用以下內容(未測驗):
使用addCategory()in 陣列存盤類別,$categories以便您可以使用getChildren().
<?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"
看到它在行動
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/383237.html
