php 查找父类所有的子类

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

1.数据库表设计

    id int(6) not null auto_increment primary key

    name varchar(20) not null ,

    pid int(6),

    sort int (6)

 

2.php代码如下:

 

class category{

 

   Static Public function parents_to_child($data,$pid=0,$level=0,$html='--'){

        

       $arr array();

       foreach($data as $v){

        

           if($v['pid'] == $pid){

               $v['level'] = $level+1;

               $v['html'] = str_repeat($html,$level);

               $arr[] = $v;

               $arr array_merge($arr,self::parents_to_child($data,$pid=$v['id'],$level=$level+1));

           }

        

       }

       return $arr;

   }

 

}

 

?>

3.在你需要分类的文件内载入类category,并引用静态方法

    require 'category.class.php';

     

    $cate = category::parents_to_child($data);

?>

以下代码是返回一个一维数组的无限分类

楼主可以根据本人所提供的代码根据自己的需求修改

转载于:https://my.oschina.net/u/588516/blog/736543

你可能感兴趣的:(php 查找父类所有的子类)