0

さて、私はここに新しく、一日中これを理解しようとしています.2つの関数が1つを呼び出していますが、私の関数は、たとえば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;
}
4

1 に答える 1

0

再帰性を使用して を返します$comment_id。私があなたのニーズを理解しているなら、1 つの記事 ID にリンクされたすべての返信 ID を取得したいと考えています。

reply_comment_count戻りますが、$comment_id再帰的に使用され、以前の ID が返されないため、最後の ID のみが取得されます。

1つだけではなく多数取得したい場合は、配列を見つけるたび$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
}

この疑似言語があなたにとって明確であることを願っています。ただし、最後に見つけたカウントのみを返すため、これしかありません。

于 2012-06-10T09:09:49.140 に答える