好的,我是新手,我整天都在尝试解决这个问题,我有两个函数,一个调用另一个,但是我的函数只返回最后一个值,例如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;
}发布于 2012-06-10 17:09:49
您可以使用递归性来返回$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
}我希望这个伪语言对您来说是清晰的。但是由于您只返回您找到的最后一个计数,所以您将只有这个计数。
https://stackoverflow.com/questions/10972433
复制相似问题