无限极分类,传递一个子ID得到所有父集,用于在前台分层显示标题

方法:

static public function getParents($data,$id){
        $arr=array();
        foreach ($data as $v) {
            if ($v['id']==$id) {
                $arr[]=$v;
                $arr=array_merge(self::getParents($data,$v['pid']),$arr);
            }
        }
        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);
        $data=Cate::getParents($data,4);
        echo '<pre>';
        print_r($data);die;
        echo '</pre>';
        $this->display();
            }
}

效果:

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