Home  >  Article  >  Backend Development  >  Examples of two infinite classification methods in PHP_PHP tutorial

Examples of two infinite classification methods in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:56:33796browse

Examples of two infinite classification methods in PHP

This article mainly introduces examples of two infinite classification methods in PHP. This article gives the infinite classification achieved by recursive and non-recursive methods. For examples, friends who need them can refer to them

1. Recursive method

Copy the code. The code is as follows:

 $items = array(

array('id'=>1,'pid'=>0,'name'=>'Level 11'),

array('id'=>2,'pid'=>0,'name'=>'Level 12'),

array('id'=>3,'pid'=>1,'name'=>'Level 21'),

array('id'=>4,'pid'=>3,'name'=>'Level 31'),

array('id'=>5,'pid'=>1,'name'=>'Level 22'),

array('id'=>6,'pid'=>3,'name'=>'Level 32'),

array('id'=>7,'pid'=>6,'name'=>'Level 41'),

 );

 $i = 0;

Function formatTree($arr, $pid = 0){

 $tree = array();

 $temp = array();

global $i;

 if($arr){

foreach($arr as $k=>$v){

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

$temp = formatTree($arr, $v['id']);

$temp && $v['son'] = $temp;

 $tree[] = $v;

 }

 }

 }

return $tree;

 }

print_r(formatTree($items));

2. Non-recursive method

Copy the code. The code is as follows:

Function genTree($items) {

 $tree = array(); //Formatted tree

 foreach ($items as $item)

 if (isset($items[$item['pid']])){

 $items[$item['pid']]['son'][] = &$items[$item['id']];

 }

else{

 $tree[] = &$items[$item['id']];

 }

return $tree;

 }

 $items = array(

 1 => array('id' => 1, 'pid' => 0, 'name' => 'Level 11'),

 2 => array('id' => 2, 'pid' => 1, 'name' => 'Level 21'),

 3 => array('id' => 3, 'pid' => 1, 'name' => 'Level 23'),

 4 => array('id' => 4, 'pid' => 9, 'name' => 'Level 31'),

 5 => array('id' => 5, 'pid' => 4, 'name' => 'Level 4 43'),

 6 => array('id' => 6, 'pid' => 9, 'name' => 'Level 32'),

 7 => array('id' => 7, 'pid' => 4, 'name' => 'Level 41'),

 8 => array('id' => 8, 'pid' => 4, 'name' => 'Level 4 42'),

 9 => array('id' => 9, 'pid' => 1, 'name' => 'Level 25'),

 10 => array('id' => 10, 'pid' => 11, 'name' => 'Level 22'),

 11 => array('id' => 11, 'pid' => 0, 'name' => 'Level 12'),

 12 => array('id' => 12, 'pid' => 11, 'name' => 'Level 24'),

 13 => array('id' => 13, 'pid' => 4, 'name' => 'Level 44'),

 14 => array('id' => 14, 'pid' => 1, 'name' => 'Level 26'),

 15 => array('id' => 15, 'pid' => 8, 'name' => 'Level 5 51'),

 16 => array('id' => 16, 'pid' => 8, 'name' => 'Level 5 52'),

 17 => array('id' => 17, 'pid' => 8, 'name' => 'Level 5 53'),

 18 => array('id' => 18, 'pid' => 16, 'name' => 'Level 6 64'),

 );

print_r(genTree($items));

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/987720.htmlTechArticleExamples of two infinite classification methods in PHP. This article mainly introduces examples of two infinite classification methods in PHP. This article gives Infinite classification examples implemented by recursive methods and non-recursive methods, friends in need...
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