遞迴調用有關問題

來源:互聯網
上載者:User
遞迴調用問題,線上等
PHP code
function show_child($pid,$layer=0){    global $db,$reply_arr,$layers,$circle_reply,$fid;    if($layer >= 5)//遞迴層數    {        return;    }    $layer++;        foreach($reply_arr as $k => $v)    {        if($v['reply'] == $pid)        {            $v['message'] = discuzcode($v['message'], $replya['smileyoff'], $replya['bbcodeoff'], $replya['htmlon'] & 1, 1, $forum['allowbbcode'], ($forum['allowimgcode'] && $showimages ? 1 : 0), $forum['allowhtml'], ($forum['jammer'] && $replya['authorid'] != $discuz_uid ? 1 : 0), 0, $replya['authorid'], $forum['allowmediacode'], $replya['pid']);                echo $v['message'].'|'.$v['pid'].'
'; show_child($v['pid']); } } }

有這樣一個函數,遞迴調用的,同時裡面又有迴圈,可是遞迴之後,應該是由於$v被重新賦值的原因吧,迴圈就不再進行了。麻煩問高手們怎麼解決?

------解決方案--------------------
試試加一句 reset($reply_arr)在迴圈前,
另外你那個$layer又在參數裡(但是調用沒用到),又global了,好像有點不妥
------解決方案--------------------
你把 show_child($v['pid']); 拿到if外面試試

------解決方案--------------------
你的意思是在第五層停止繼續進行遞迴是不是?
如果是的話可以這麼實現。
如果是我理解錯了,就當我沒有來過這裡。

PHP code
function show_child($pid,$layer=0){    if($layer < 5) {        echo "當前是第".($layer+1)."層迴圈
\n"; show_child($pid, ++$layer); } else { return false; }}
------解決方案--------------------
不要有問題就想到問人家 你就是解決問題的最佳人選啊
送你一套 最完整的 php 教程 的 ppt 那裡面 你可以解決你
遇到的所有問題
相信自己 自己就是解決問題的主人http://http://www.itkanba.com/bbs/viewthread.php?tid=9508&extra=
------解決方案--------------------
PHP code
function show_child($pid,$layer=0) {    global $db,$reply_arr,$layers,$circle_reply,$fid;    if($layer >= 5)//遞迴層數     { return; } $layer++;    foreach($reply_arr as $k => $v) {        if($v['reply'] == $pid) {          echo $v['message'].'|'.$v['pid'].'
'; show_child($v['pid'], 7); // here, you want to stop this loop , and // add the second parameter that is more than 5. } } }
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.