首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP函数返回意外的值

问PHP函数返回意外的值
EN

Stack Overflow用户
提问于 2012-06-10 14:36:36
回答 1查看 95关注 0票数 0

好的,我是新手,我整天都在尝试解决这个问题,我有两个函数,一个调用另一个,但是我的函数只返回最后一个值,例如29,当它应该返回多个值的时候。我想知道如何解决这个问题,以便我的函数返回所有的值。

这是我的PHP代码。

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2012-06-10 17:09:49

您可以使用递归性来返回$comment_id。如果我理解你的需求,你想让每个回复id链接到一个文章id。

在reply_comment_count中,您返回$comment_id,但是由于它被临时使用,并且您没有保留返回的前一个id,所以您只获得最后一个id。

如果你想得到更多的$comment_id而不是只有一个,我建议你返回一个数组,当你找到一个数组的时候就推送$comment_id。大概是这样的:

代码语言:javascript
复制
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
}

我希望这个伪语言对您来说是清晰的。但是由于您只返回您找到的最后一个计数,所以您将只有这个计数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10972433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档