github 地址:https://github.com/BlueM/Tree
假如数据为:$data = [...];
初始化:
$tree = new BlueM\Tree(
$data,
['rootId' => -1, 'id' => 'nodeId', 'parent' => 'parentId']
);
常见用法:
// Rebuild the tree from new data
$tree->rebuildWithData($newData);
// Get the top-level nodes (returns array)
$rootNodes = $tree->getRootNodes();
// Get all nodes (returns array)
$allNodes = $tree->getNodes();
// Get a single node by its unique identifier
$node = $tree->getNodeById(12345);
额定
这个插件默认转 json 的话,只会输入一层数据,如果想返回多层数据,须要在初始化的时候多加一个参数:
$tree = new Tree($data, ['jsonSerializer' => new Tree\Serializer\HierarchicalTreeJsonSerializer()]