首页 > 开发 > PHP > 正文

PHPTree——php快速生成无限级分类

2024-05-04 22:43:42
字体:
来源:转载
供稿:网友

它就是PHPTree。

git地址:https://git.oschina.net/jiusem/PHPTree.git

或从错新站长站下载 //www.jb51.net/codes/606002.html

最简单的例子:

<?php require('PHPTree.class.php');//原始数据, 从数据库读出$data = array(	array(		'id'=>1,		'name'=>'book',		'parent_id'=>0	),	array(		'id'=>2,		'name'=>'music',		'parent_id'=>0	),	array(		'id'=>3,		'name'=>'book1',		'parent_id'=>1	),	array(		'id'=>4,		'name'=>'book2',		'parent_id'=>3	));$r = PHPTree::makeTree($data);echo json_encode($r);?>

输出:

  [
        {
            "id": 1,
            "name": "book",
            "parent_id": 0,
            "expanded": false, //不展开子节点
            "children": [
                {
                    "id": 3,
                    "name": "book1",
                    "parent_id": 1,
                    "expanded": false,
                    "children": [
                        {
                            "id": 4,
                            "name": "book2",
                            "parent_id": 3,
                            "leaf": true
                        }
                    ]

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表