推荐一个PHP Tree无限级分类组件 BlueM/Tree

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()]

你可能感兴趣的:(php)