PHP无限极分类

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-2-1
 * Time: 下午11:39
 */
namespace Home\Controller;

use Think\Controller;

class CateController extends Controller{
    public function index(){
        $arr = array(
            array(
                'id' => 1,
                'name' => 'PHP',
                'pid' => 0,
                'path' => '0'
            ),
            array(
                'id' => 2,
                'name' => 'JAVA',
                'pid' => 0,
                'path' => '0'
            ),
            array(
                'id' =>3,
                'name' => 'ThinkPHP',
                'pid' => 1,
                'path' => '0-1'
            ),
            array(
                'id' =>4,
                'name' => 'Spring',
                'pid' => 2,
                'path' => '0-2'
            ),
        );
        foreach($arr as &$arr2){
            $num = substr_count($arr2['path'],'-');
            if($arr2['pid'] > 0){
                $pre = '|'.str_repeat('-',$num);
            }else{
                $pre = '';
            }
            $arr2['tree'] = $pre.$arr2['name'];
        }

        foreach($arr as $arr3){
            $rows[] = $arr3['path'].'-'.$arr3['id'];
        }
        array_multisort($rows,$arr);//多数组排序
       // print_r($arr);
        $this->assign('arr',$arr);
        $this->display();
    }
}
?>

 

posted @ 2016-02-02 00:20  FinnYY  阅读(187)  评论(0编辑  收藏  举报