首頁  >  文章  >  後端開發  >  php無限分類樹[支援子分類排序]

php無限分類樹[支援子分類排序]

PHP中文网
PHP中文网原創
2017-09-01 16:47:071543瀏覽

沒有人規定一定要使用類,你寫成一個函數也可以,每個人的程式設計習慣、風格、喜好都不一樣,關於封裝成類是為了使這段程式具有結構條理性,而不是隨意放置,關於類別中包含的多個方法(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