нужен такой вид данных
$data = [
['title' => 'Node 1', 'key' => 1],
['title' => 'Folder 2', 'key' => '2', 'folder' => true, 'children' => [
['title' => 'Node 2.1', 'key' => '3'],
['title' => 'Node 2.2', 'key' => '4']
]]
];
из этих данных:
есть такой код но строит не совсем правильно:
public $depthAttribute = 'parent_id';
/**
* @var string
*/
public $labelAttribute = 'name';
/**
* @var string
*/
public $childrenOutAttribute = 'children';
/**
* @var string
*/
public $labelOutAttribute = 'title';
/**
* @var string
*/
public $hasChildrenOutAttribute = 'folder';
/**
* @var string
*/
public $makeLinkCallable = null;
public function tree()
{
$makeNode = function ($node) {
$newData = [
$this->labelOutAttribute => $node[$this->labelAttribute],
];
if (is_callable($makeLink = $this->makeLinkCallable)) {
$newData += [
$this->hrefOutAttribute => $makeLink($node),
];
}
return array_merge($node, $newData);
};
// Trees mapped
$trees = array();
$collection = $this->owner->find()->asArray()->all();
if (count($collection) > 0) {
foreach ($collection as &$col) $col = $makeNode($col);
// Node Stack. Used to help building the hierarchy
$stack = array();
foreach ($collection as $node) {
$item = $node;
$item[$this->childrenOutAttribute] = array();
// Number of stack items
$l = count($stack);
// Check if we're dealing with different levels
while ($l > 0 && $stack[$l - 1][$this->depthAttribute] >= $item[$this->depthAttribute]) {
array_pop($stack);
$l--;
}
// Stack is empty (we are inspecting the root)
if ($l == 0) {
// Assigning the root node
$i = count($trees);
$trees[$i] = $item;
$stack[] = &$trees[$i];
} else {
// Add node to parent
$i = count($stack[$l - 1][$this->childrenOutAttribute]);
$stack[$l - 1][$this->hasChildrenOutAttribute] = true;
$stack[$l - 1][$this->childrenOutAttribute][$i] = $item;
$stack[] = &$stack[$l - 1][$this->childrenOutAttribute][$i];
}
}
}
return $trees;
}