好的我是新来的,我一直在试图解决这个问题,我有两个函数,一个调用另一个,但我的函数只返回示例29
的最后一个值,它应该返回多个值。我想知道如何解决这个问题,以便我的函数返回所有值。
这是我的PHP代码。
function parent_comments(){
if(articles_parent_comments_info($_GET['article_id']) !== false){
foreach(articles_parent_comments_info($_GET['article_id']) as $comment_info){
$comment_id = filternum($comment_info['comment_id']);
reply_comment_count($comment_id);
}
}
}
function reply_comment_count($parent_id){
if(articles_reply_comments_info($_GET['article_id']) !== false){
foreach(articles_reply_comments_info($_GET['article_id']) as $reply_info){
$comment_id = filternum($reply_info['comment_id']);
$reply_id = filternum($reply_info['parent_id']);
if($parent_id === $reply_id){
reply_comment_count($comment_id);
}
}
}
return $comment_id;
}
答案 0 :(得分:0)
您使用递归来返回$comment_id
。如果我了解您的需求,您希望将每个回复ID链接到一个文章ID。
在reply_comment_count
中,您返回$comment_id
,但由于它以递归方式使用,并且您没有保留之前返回的ID,因此只能获得最后一个ID。
如果你想获得多个$comment_id
而不是只有一个,我建议你返回一个数组,每当你找到一个数组时你就推$comment_id
。这样的事情:
func parent_comments(){
loop in articles to get comment_id {
count_array = reply_comment_count(comment_id, count_array)
}
}
func reply_comment_count(parent_id, count_array) {
loop to get id linked to parent_id {
if id is an article {
count_array = reply_comment_count(id, count_array) #recursive call
}
else {
count_comment = count comment linked
count_array.push(count_comment)
}
}
return count_array # when you return your count_array due to recursive call it will be filled with every count, and not only the last
}
我希望这个伪语言对你来说很清楚。但是,由于您只返回您找到的最后一个计数,因此您只能使用此计数。