• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

简单好用的PHP无限分类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

/**
* author: askie
* blog: http://www.pkphp.com
* 版权: 随便用
* 无限分类
*/
class Tree
{
        public $data=array();
        public $cateArray=array();
       
        function Tree()
        {
              
        }
        function setNode ($id, $parent, $value)
    {
        $parent = $parent?$parent:0;
        $this->data[$id]                = $value;
        $this->cateArray[$id]        = $parent;
    }
    function getChildsTree($id=0)
    {
            $childs=array();
            foreach ($this->cateArray as $child=>$parent)
            {
                    if ($parent==$id)
                    {
                            $childs[$child]=$this->getChildsTree($child);
                    }
                   
            }
            return $childs;
    }
    function getChilds($id=0)
    {
            $childArray=array();
            $childs=$this->getChild($id);
            foreach ($childs as $child)
            {
                    $childArray[]=$child;
                    $childArray=array_merge($childArray,$this->getChilds($child));
            }
            return $childArray;
    }
    function getChild($id)
    {
            $childs=array();
            foreach ($this->cateArray as $child=>$parent)
            {
                    if ($parent==$id)
                    {
                            $childs[$child]=$child;
                    }
            }
            return $childs;
    }
    //单线获取父节点
    function getNodeLever($id)
    {
            $parents=array();
            if (key_exists($this->cateArray[$id],$this->cateArray))
            {
                    $parents[]=$this->cateArray[$id];
                    $parents=array_merge($parents,$this->getNodeLever($this->cateArray[$id]));
            }
            return $parents;
    }
    function getLayer($id,$preStr='|-')
    {
            return str_repeat($preStr,count($this->getNodeLever($id)));
    }
    function getValue ($id)
    {
        return $this->data[$id];
    } // end func
}

/*$Tree = new Tree("请选择分类");
//setNode(目录ID,上级ID,目录名字);
$Tree->setNode(1, 0, '目录1');
$Tree->setNode(2, 1, '目录2');
$Tree->setNode(5, 3, '目录5');
$Tree->setNode(3, 0, '目录3');
$Tree->setNode(4, 2, '目录4');
$Tree->setNode(9, 4, '目录9');
$Tree->setNode(6, 2, '目录6');
$Tree->setNode(7, 2, '目录7');
$Tree->setNode(8, 3, '目录8');

//print_r($Tree->getChildsTree(0));
//print_r($Tree->getChild(0));
//print_r($Tree->getLayer(2));

$category = $Tree->getChilds();

//遍历输出
foreach ($category as $key=>$id)
{
    echo $id.$Tree->getLayer($id, '|-').$Tree->getValue($id)."\n";
}*/

?>


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP7学习笔记(三)关于PHP7如何安装调试工具Xdebug扩展以及Zephir的问题 ...发布时间:2022-07-10
下一篇:
php时间段判断发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap