首页  >  文章  >  后端开发  >  php无限分类树[支持子分类排序]

php无限分类树[支持子分类排序]

PHP中文网
PHP中文网原创
2017-09-01 16:47:071520浏览

没有人规定一定要使用类,你写成一个函数也可以,每个人的编程习惯、风格、喜好都不一样,关于封装成类是为了使这段程序具有结构条理性,而不是随意放置,关于类中包含的多个方法(vTree、hTree)主要应对不同业务场景,vTree为单线纵向结构,而hTree为树形结构。

ClassTree.class.php

<?php
/**
 * 无限分类树(支持子分类排序)
 * version:1.4
 * author:Veris
 * website:www.mostclan.com
 */
class ClassTree {
    /**
     * 分类排序(降序)
     */
    static public function sort($arr,$cols){
        //子分类排序
        foreach ($arr as $k => &$v) {
            if(!empty($v[&#39;sub&#39;])){
                $v[&#39;sub&#39;]=self::sort($v[&#39;sub&#39;],$cols);
            }
            $sort[$k]=$v[$cols];
        }
        if(isset($sort))
            array_multisort($sort,SORT_DESC,$arr);
        return $arr;
    }
    /**
     * 横向分类树
     */
    static public function hTree($arr,$pid=0){
        foreach($arr as $k => $v){
            if($v[&#39;pid&#39;]==$pid){
                $data[$v[&#39;id&#39;]]=$v;
                $data[$v[&#39;id&#39;]][&#39;sub&#39;]=self::hTree($arr,$v[&#39;id&#39;]);
            }
        }
        return isset($data)?$data:array();
    }
    /**
     * 纵向分类树
     */
    static public function vTree($arr,$pid=0){
        foreach($arr as $k => $v){
            if($v[&#39;pid&#39;]==$pid){
                $data[$v[&#39;id&#39;]]=$v;
                $data+=self::vTree($arr,$v[&#39;id&#39;]);
            }
        }
        return isset($data)?$data:array();
    }
}

返回例子:

Array
(
    [4] => Array
        (
            [id] => 4
            [pid] => 0
            [name] => 上海
            [sort] => 2
        )

    [5] => Array
        (
            [id] => 5
            [pid] => 4
            [name] => 闵行
            [sort] => 0
        )

    [1] => Array
        (
            [id] => 1
            [pid] => 0
            [name] => 浙江
            [sort] => 0
        )

    [13] => Array
        (
            [id] => 13
            [pid] => 1
            [name] => 金华
            [sort] => 1
        )

    [10] => Array
        (
            [id] => 10
            [pid] => 1
            [name] => 宁波
            [sort] => 0
        )

    [6] => Array
        (
            [id] => 6
            [pid] => 10
            [name] => 宁海
            [sort] => 0
        )

)
Array
(
    [4] => Array
        (
            [id] => 4
            [pid] => 0
            [name] => 上海
            [sort] => 2
            [sub] => Array
                (
                    [5] => Array
                        (
                            [id] => 5
                            [pid] => 4
                            [name] => 闵行
                            [sort] => 0
                            [sub] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 1
            [pid] => 0
            [name] => 浙江
            [sort] => 0
            [sub] => Array
                (
                    [13] => Array
                        (
                            [id] => 13
                            [pid] => 1
                            [name] => 金华
                            [sort] => 1
                            [sub] => Array
                                (
                                )

                        )

                    [10] => Array
                        (
                            [id] => 10
                            [pid] => 1
                            [name] => 宁波
                            [sort] => 0
                            [sub] => Array
                                (
                                    [6] => Array
                                        (
                                            [id] => 6
                                            [pid] => 10
                                            [name] => 宁海
                                            [sort] => 0
                                            [sub] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)
public function vTree($arr,$pid=0){
foreach($arr as $k => $v){
if($v[&#39;pid&#39;]==$pid){
$data[$v[&#39;id&#39;]]=$v;
$data+=vTree($arr,$v[&#39;id&#39;]);
}
}
return isset($data)?$data:array();
}


以上是php无限分类树[支持子分类排序]的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn