Home  >  Article  >  Backend Development  >  PHP recursive method to implement infinite classification examples_PHP tutorial

PHP recursive method to implement infinite classification examples_PHP tutorial

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

Array:

The code is as follows
 代码如下
$items = array(
 array('id' => 1, 'pid' => 0, 'name' => '一级11' ),
 array('id' => 11, 'pid' => 0, 'name' => 'www.111cn.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' ),
);
$items = array(
array('id' => 1, 'pid' => 0, 'name' => 'Level 11' ),
array(' id' => 11, 'pid' => 0, 'name' => 'www.111cn.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 2 25' ),
);

Function:
 代码如下
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 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']);
                                                                                                     ​ Array
$tem && $v['son'] = $tem;
$arr[] = $v;
}
}
return $arr;}
    Among them, the array must contain id and pid to specify the hierarchical relationship between the array values ​​
  • ​ Articles you may be interested in
  • Implementation of infinite classification & tree forum
  • php mysql implements unlimited classification and displays classification relationships in tree format
  • Unlimited categories of articles
  • Unlimited categories
  • How to implement unlimited classification & tree forum
  • php unlimited classification code
  • php infinite level classification [recursive method]
  • Infinite level classification [Pre-sorted tree traversal algorithm]
  • PHP infinite classification algorithm
Unlimited classification codes for ecshop products

http://www.bkjia.com/PHPjc/738522.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/738522.htmlTechArticle
Array: The code is as follows $items = array( array('id' = 1, 'pid' = 0, ' name' = 'Level 11' ), array('id' = 11, 'pid' = 0, 'name' = 'www.111cn.net Level 12' ), array('id' = 2, 'pid ' = 1,...
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