class Tree {

	// 展示数据
	public function index(){
		
		$data = $this->ex_tree(0);
		 
		print_r($data);
	}
	
	// 递归的实现
	public function ex_tree($pid,&$data=NULL){
		$child_data = $this->_find_child($pid);
		 
		foreach ($child_data as $item){
			$item ['child'] = $this->ex_tree($item['id'],$item['child']);
			
			$data[$item['id']] = $item;
		}
		
		return $data;
	}
	
	private function _find_child($parentid ){
		$rows = array(
				array(
						'id' => 1,
						'name' => '服装',
						'parentid' => 0
				),
				array(
						'id' => 2,
						'name' => '美食',
						'parentid' => 0
				),
				array(
						'id' => 3,
						'name' => '电器',
						'parentid' => 0
				),
				array(
						'id' => 4,
						'name' => '衣服',
						'parentid' => 1
				),
				array(
						'id' => 5,
						'name' => '裤子',
						'parentid' => 1
				),
				array(
						'id' => 6,
						'name' => '板栗',
						'parentid' => 2
				),
				array(
						'id' => 7,
						'name' => '风扇',
						'parentid' => 3
				),
				array(
						'id' => 8,
						'name' => '运动裤',
						'parentid' => 5
				),
				array(
						'id' => 9,
						'name' => '九分裤',
						'parentid' => 5
				),
				array(
						'id' => 10,
						'name' => '首饰',
						'parentid' => 0
				)
		);
		$return = array();
		foreach ($rows as $item){
			if($item['parentid'] == $parentid){
				$return[$item['id']] = $item;
			}
		}
		return $return;
	}
	
	
}

// 运行
$t = new Tree();
$t -> index();


输出:

Array
(
    [1] => Array
        (
            [id] => 1
            [name] => 服装
            [parentid] => 0
            [child] => Array
                (
                    [4] => Array
                        (
                            [id] => 4
                            [name] => 衣服
                            [parentid] => 1
                            [child] => 
                        )

                    [5] => Array
                        (
                            [id] => 5
                            [name] => 裤子
                            [parentid] => 1
                            [child] => Array
                                (
                                    [8] => Array
                                        (
                                            [id] => 8
                                            [name] => 运动裤
                                            [parentid] => 5
                                            [child] => 
                                        )

                                    [9] => Array
                                        (
                                            [id] => 9
                                            [name] => 九分裤
                                            [parentid] => 5
                                            [child] => 
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [id] => 2
            [name] => 美食
            [parentid] => 0
            [child] => Array
                (
                    [6] => Array
                        (
                            [id] => 6
                            [name] => 板栗
                            [parentid] => 2
                            [child] => 
                        )

                )

        )

    [3] => Array
        (
            [id] => 3
            [name] => 电器
            [parentid] => 0
            [child] => Array
                (
                    [7] => Array
                        (
                            [id] => 7
                            [name] => 风扇
                            [parentid] => 3
                            [child] => 
                        )

                )

        )

    [10] => Array
        (
            [id] => 10
            [name] => 首饰
            [parentid] => 0
            [child] => 
        )

)


你可能感兴趣的文章