无限极分类,把子集数组压到父集数组的一个子项下面,用于在前台模板更好的循环显示

方法代码:

    static public function unlimitedForLayer($data,$pid=0){
        $arr=array();
        foreach ($data as $v) {
            if ($v['pid']==$pid) {
                $v['child']=self::unlimitedForLayer($data,$v['id']);
                $arr[]=$v;
            }
        }
        return $arr;
    }

调用程序:

<?php
namespace Home\Controller;
use Think\Controller;
use Home\Clas\Cate;
class IndexController extends Controller {
    public function index(){
        $data=array(
                '0'=>array(
                        'id'=>1,
                        'pid'=>0
                    ),
                '1'=>array(
                        'id'=>2,
                        'pid'=>0
                    ),
                '2'=>array(
                        'id'=>3,
                        'pid'=>1
                    ),
                '3'=>array(
                        'id'=>4,
                        'pid'=>3
                    )
            );
        //$data=Cate::unlimitedForLevel($data);
        $data=Cate::unlimitedForLayer($data);
        echo '<pre>';
        print_r($data);die;
        echo '</pre>';
        $this->display();
            }
}

效果图:

posted @ 2016-01-10 15:37  屌丝IT男  阅读(518)  评论(0编辑  收藏  举报