[英]How do I randomly jumble a string imploded by ',' in PHP (ex: a,b,c = c,b,a) in a oneliner?
[英]PHP : How can i get the result "B,C" or "C,B"
嗨,有人帮我解决这个问题,在此先感谢
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”。
您可以使用以下内容(未经测试):
使用addCategory()
将类别存储在数组$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"
看到它在行动
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.