private function buildTree($nodes)
{
$data = [];
foreach ($nodes as $node) {
$array = [
'name' => $node->getName(),
];
if (!$node->isLeaf()) {
$array['collapsed'] = false;
$array['children'] = self::buildTree($node->getChildren());
}
array_push($data, $array);
}
return $data;
}
树型数据处理,其实想法就是递归,先把条件递过去,处理之后将数据一层一层的归来
以下是一个更易懂的,根据父级id获取数据的例子
public function dataRecTree($parent_id = 0, &$result=[])
{
$root_son = $this->getSonDrama($parent_id);
if (!empty($root_son)) {
foreach ($root_son as $key => $value) {
$value['child'] = $this->dataRec($value['id']);
$result[] = $value;
}
}
return $result;
}
0 条评论