Home Backend Development PHP Tutorial 递归调用有关问题

递归调用有关问题

Jun 13, 2016 pm 01:40 PM
child pid reply

递归调用问题,在线等

PHP code

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

<!--

 

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

 

-->function show_child($pid,$layer=0)

{

    global $db,$reply_arr,$layers,$circle_reply,$fid;

    if($layer &gt;= 5)//递归层数

    {

        return;

    }

    $layer++;

     

    foreach($reply_arr as $k =&gt; $v)

    {

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

        {

            $v['message'] = discuzcode($v['message'], $replya['smileyoff'], $replya['bbcodeoff'], $replya[

'htmlon'] &amp; 1, 1, $forum['allowbbcode'], ($forum['allowimgcode'] &amp;&amp; $showimages ? 1 : 0), $forum['allowhtml'], ($forum['jammer'] &amp;&amp; $replya['authorid'] != $discuz_uid ? 1 : 0), 0, $replya['authorid'], $forum['allowmediacode'], $replya['pid']);

     

            echo $v['message'].'|'.$v['pid'].'<br>';

            show_child($v['pid']);

        }

    }

     

}

Copy after login

有这样一个函数,递归调用的,同时里面又有循环,可是递归之后,应该是由于$v被重新赋值的原因吧,循环就不再进行了。麻烦问高手们怎么解决?

------解决方案--------------------
试试加一句 reset($reply_arr)在循环前,
另外你那个$layer又在参数里(但是调用没用到),又global了,好像有点不妥
------解决方案--------------------
你把 show_child($v['pid']); 拿到if外面试试

------解决方案--------------------
你的意思是在第五层停止继续进行递归是不是?
如果是的话可以这么实现。
如果是我理解错了,就当我没有来过这里。

PHP code

1

2

3

4

5

6

7

8

9

10

11

function show_child($pid,$layer=0)

{

    if($layer \n";

        show_child($pid, ++$layer);

    }

    else {

        return false;

    }

}

<br><font color="#e78608">------解决方案--------------------</font><br>不要有问题就想到问人家 你就是解决问题的最佳人选啊  <br>送你一套 最完整的 php 教程 的 ppt 那里面 你可以解决你  <br>遇到的所有问题  <br>相信自己 自己就是解决问题的主人http://http://www.itkanba.com/bbs/viewthread.php?tid=9508&amp;extra=

<br><font color="#e78608">------解决方案--------------------</font><br>

Copy after login
PHP code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

function show_child($pid,$layer=0) {

   global $db,$reply_arr,$layers,$circle_reply,$fid;

   if($layer &gt;= 5)//递归层数 

   { return; } $layer++;

 

   foreach($reply_arr as $k =&gt; $v) {

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

          echo $v['message'].'|'.$v['pid'].'<br>';

          show_child($v['pid'], 7); // here, you want to stop this loop , and

                                    // add the second parameter that is more than 5.

       }

   }

} <div class="clear">

                  

               

               

         

            </div>

Copy after login
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles