getParentId(); if ($pid == null || $pid == 0) { $topNodes[] = $children; continue; } foreach ($nodes as $parent) { /** @var Tree $parent */ $id = $parent->getId(); if ($id != null && $id == $pid) { $parent->addChildren($children); $children->setHasParent(true); $parent->setHasChildren(true); continue; } } } $root = new Tree(); if (sizeof($topNodes) == 1) { $root = $topNodes[0]; } else { $root->setId("-1"); $root->setParentId(""); $root->setHasParent(false); $root->setHasChildren(true); $root->setChecked(true); $root->setChildren($topNodes); $root->setText("顶级节点"); $root->setState(["opened" => true]); } return $root; } }