递归,无限极分类的两种方法

$this ->GetTree( $cate , ‘’ , ‘’,‘’ );
public function make_tree1( $list , $pk = 'id' , $pid = 'pid' , $child = '_child' , $root = 0 ){
$tree = array ();
foreach ( $list as $key => $val ){
if ( $val [ $pid ]== $root ){
//获取当前$pid所有子类
unset ( $list [ $key ]);
if (! empty ( $list )){
$child = $this ->make_tree1( $list , $pk , $pid , $child , $val [ $pk ]); //来来来 递归 空

if (! empty ( $child )){
$val [ '_child' ]= $child ;
}
}
$tree []= $val ;
}
}
return $tree ;
}

$a = $this ->GetTree( $cate , 0 , 0 );
private function GetTree( $arr , $pid , $step ){
global $tree ;
foreach ( $arr as $key => $val ) {
if ( $val [ 'parent' ] == $pid ) {
$flg = str_repeat ( '└―' , $step );
$val [ 'business_name' ] = $flg . $val [ 'business_name' ];
$tree [] = $val ;
$this ->GetTree( $arr , $val [ 'business_id' ] , $step + 1 );
}
}
return $tree ;
}

你可能感兴趣的:(知识点)