Home  >  Article  >  Backend Development  >  PHP implements infinite classification example code (recursive)

PHP implements infinite classification example code (recursive)

怪我咯
怪我咯Original
2017-07-07 09:51:14928browse

This article mainly introduces the phprecursive method to achieve infinite classification example code. Friends who need it can refer to the

array:

The code is as follows:

$items = array(
 array('id' => 1, 'pid' => 0, 'name' => '一级11' ),
 array('id' => 11, 'pid' => 0, 'name' => 'www.jb51.net 一级12' ),
 array('id' => 2, 'pid' => 1, 'name' => '二级21' ),
 array('id' => 10, 'pid' => 11, 'name' => '二级22' ),
 array('id' => 3, 'pid' => 1, 'name' => '二级23' ),
 array('id' => 12, 'pid' => 11, 'name' => '二级24' ),
 array('id' => 13, 'pid' => 12, 'name' => '三级31' ),
 array('id' => 9, 'pid' => 1, 'name' => '二级25' ),
);

Function:

The code is as follows:

function formatTree($array, $pid = 0){
 $arr = array();
 $tem = array();
 foreach ($array as $v) {
  if ($v['pid'] == $pid) {
   $tem = formatTree($array, $v['id']);
                        //判断是否存在子数组
   $tem && $v['son'] = $tem;
   $arr[] = $v;
  }
 }
 return $arr;
}


The above is the detailed content of PHP implements infinite classification example code (recursive). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn