Home  >  Article  >  Backend Development  >  PHP recursive method to achieve infinite classification example code_PHP tutorial

PHP recursive method to achieve infinite classification example code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:36:59840browse

Array:

Copy code The code is as follows:

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

Function:

Copy code 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']);
// determine whether there is a sub -array
$ TEM && $ v v;
}
}
return $arr;
}

http://www.bkjia.com/PHPjc/736833.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736833.htmlTechArticleArray: Copy the code as follows: $items = array( array('id' = 1, 'pid' = 0, 'name' = 'Level 11' ), array('id' = 11, 'pid' = 0, 'name' = 'www.jb51.net Level 12' ), array('id' =. ..
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