PHP - 从递归函数返回值

时间:2014-07-02 08:57:27

标签: php codeigniter

我的功能如下:

function show_comments(&$comments, $parent_id = 0) {
      $comments_list = "<ul>\n";
      foreach($comments as $comment) :
        if ($comment["parent_id"] != $parent_id)
          continue;
        $comments_list .= "<li>\n<h2>{$comment['id_comment']}</h2>\n";
        $comments_list .= "<p>$comment[body]</p>\n";


        $this->show_comments($comments, $comment['id_comment']);
        $comments_list .= "</li>\n";
      endforeach;
      $comments_list .= "</ul>\n";

      return $comments_list;
    }

此功能只返回一个结果(第一个)。如何绑定所有结果并返回它们?

1 个答案:

答案 0 :(得分:2)

在第10行,您调用show_comments()但不要连接结果。

$comments_list .= $this->show_comments($comments, $comment['id_comment']);