1. 程式人生 > 其它 >ThinkPHP無限級分類(遞迴)

ThinkPHP無限級分類(遞迴)

程式碼演示

沒什麼可說的直接看程式碼

<?php
namespace app\controller;
class Category
{
    //模擬假資料
    protected static function arr()
    {
        $rows = [
            [
                'id' => '1',
                'name' => '一級選單',
                'pid' => '0',
                'path' => '0',
            ],
            [
                'id' => '2',
                'name' => '二級選單',
                'pid' => '0',
                'path' => '0',
            ],
            [
                'id' => '3',
                'name' => '一級選單-1',
                'pid' => '1',
                'path' => '0-1',
            ],
            [
                'id' => '4',
                'name' => '二級選單-1',
                'pid' => '2',
                'path' => '0-2',
            ],
            [
                'id' => '5',
                'name' => '一級選單-1-1',
                'pid' => '3',
                'path' => '0-1-3',
            ],
            [
                'id' => '6',
                'name' => '二級選單-1-1',
                'pid' => '4',
                'path' => '0-2-4',
            ],
            [
                'id' => '7',
                'name' => '二級選單-1-2',
                'pid' => '4',
                'path' => '0-2-4',
            ],
            [
                'id' => '8',
                'name' => '三級選單',
                'pid' => '0',
                'path' => '0',
            ],
            [
                'id' => '9',
                'name' => '二級選單-1-3',
                'pid' => '4',
                'path' => '0-2-4-6',
            ],
            [
                'id' => '10',
                'name' => '三級選單-1',
                'pid' => '8',
                'path' => '0-8',
            ],
            [
                'id' => '11',
                'name' => '一級選單-1-4',
                'pid' => '5',
                'path' => '0-8',
            ],
        ];
        return $rows;
    }

    public function index()
    {
        //獲取從第0級下的所有分類(pid==0)
        $list = $this->build_tree(0);
        return json($list);
    }

    /**
     * 遞迴子級
     * @param $list
     * @param $id
     * @return array
     */
    public function findChild($list, $id)
    {
        $child = [];
        foreach ($list as $key => $item) {
            //如果pid等於傳進來
            if ($item['pid'] == $id) {
                $child[] = $item;
            }
        }
        return $child;
    }
    /**
     * 獲取當前級別的子級
     * @param $root_id /第幾層
     * @return null
     */
    public function build_tree($root_id)
    {
        //獲取假資料
        $list = $this->arr();
        //查詢指定級數
        $tree = $this->findChild($list, $root_id);
        if (empty($tree)) {
            return null;
        }
        //遍歷獲取到的層級得到下級分類
        foreach ($tree as $key => $item) {
            //遞迴呼叫自己 查詢每個元素下的分類
            $child = $this->build_tree($item['id']);
            //如果有子類就放入陣列中
            if ($child != null){
                $tree[$key]['child'] = $child;
            }
        }
        return $tree;
    }
} 

預覽結果