Home > Backend Development > PHP Tutorial > How to implement recursion and infinite classification in PHP_PHP Tutorial

How to implement recursion and infinite classification in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:04:12
Original
746 people have browsed it

How to implement recursion and infinite classification in php

This article mainly introduces the method of implementing recursion and infinite classification in php, involving recursive operation skills in php, friends who need it can refer to it Next

The example in this article describes the method of implementing recursion and infinite classification in PHP, and shares it with everyone for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

echo "

";

$area = array(

array('id'=>1,'area'=>'北京','pid'=>0),

array('id'=>2,'area'=>'广西','pid'=>0),

array('id'=>3,'area'=>'广东','pid'=>0),

array('id'=>4,'area'=>'福建','pid'=>0),

array('id'=>11,'area'=>'朝阳区','pid'=>1),

array('id'=>12,'area'=>'海淀区','pid'=>1),

array('id'=>21,'area'=>'南宁市','pid'=>2),

array('id'=>45,'area'=>'福州市','pid'=>4),

array('id'=>113,'area'=>'亚运村','pid'=>11),

array('id'=>115,'area'=>'奥运村','pid'=>11),

array('id'=>234,'area'=>'武鸣县','pid'=>21)

);

 

function t($arr,$pid=0,$lev=0){

static $list = array();

foreach($arr as $v){

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

echo str_repeat(" ",$lev).$v['area']."
";

//这里输出,是为了看效果

$list[] = $v;

t($arr,$v['id'],$lev+1);

}

}

return $list;

}

$list = t($area);

echo "


";

print_r($list);

?>

1 2

3

4 5

67 8 9 10 11 12
13
14
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<🎜>echo "
";
            $area = array(
            array('id'=>1,'area'=>'Beijing','pid'=>0),
            array('id'=>2,'area'=>'Guangxi','pid'=>0),
            array('id'=>3,'area'=>'Guangdong','pid'=>0),
            array('id'=>4,'area'=>'Fujian','pid'=>0),
            array('id'=>11,'area'=>'Chaoyang District','pid'=>1),
            array('id'=>12,'area'=>'Haidian District','pid'=>1),
            array('id'=>21,'area'=>'Nanning City','pid'=>2),
            array('id'=>45,'area'=>'Fuzhou City','pid'=>4),
            array('id'=>113,'area'=>'Asian Games Village','pid'=>11),
            array('id'=>115,'area'=>'Olympic Village','pid'=>11),
            array('id'=>234,'area'=>'Wuming County','pid'=>21)
            );
             
            function t($arr,$pid=0,$lev=0){
            static $list = array();
            foreach($arr as $v){
            if($v['pid']==$pid){
            echo str_repeat(" ",$lev).$v['area']."
"; //Output here is to see the effect $list[] = $v; t($arr,$v['id'],$lev+1); } } return $list; } $list = t($area); echo "
"; print_r($list); ?>
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/966908.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/966908.htmlTechArticleHow to implement recursion and infinite classification in PHP This article mainly introduces the method of implementing recursion and infinite classification in PHP, involving Friends who need PHP's recursive operation skills can refer to the examples in this article...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template