自动嵌套循环

I am building a comments system and I am trying to create a reddit esque system - the comments are not bounded by a pre-determined value. However, I cannot seem to automate this process, the logic is not working in my head.

To display comments I loop through the displayed comments that are not children to any other comments. I then query a function to search for children of each parent comment at each iteration. The problem with this is that the only method for me to have an infinity comment system is to constantly put loops within loops.

Am I missing something? I hope to be able to create a function that can contain a loop within a loop then random generate the variable name to ensure that it is never overwriting...but if I am off-track then can someone point me in the right direction.

$iterator = 0; //Used to ensure the ID's remain unique 
$articleID = $_GET['POST']; //Get article ID from the URL
$newsArticleComments = getComment($articleID); // Get news article where ID matches ID found in URL
$newsArticleCommentsJSON = json_decode($newsArticleComments); // Decode JSON returned by getNewsArticleFromID function

if ($newsArticleComments != "[]") {
    for ($i = 0; $i < sizeof($newsArticleCommentsJSON); $i++) {
        if ($newsArticleCommentsJSON[$i]->CommentReply <= 0) {
            if ($newsArticleCommentsJSON[$i]->CommentApproved >= 1) {
                displayReply($newsArticleCommentsJSON, $i, FALSE, $iterator);
                replyForm(true, $iterator, $newsArticleCommentsJSON, $i);
                $iterator++;
                //Get Comment Replies for each sub comment (Here it is checked)
                $newsArticleCommentsReplies = getCommentReplies($articleID, $newsArticleCommentsJSON[$i]->CommentId); // Get news article where ID matches ID found in URL
                $newsArticleCommentsRepliesJSON = json_decode($newsArticleCommentsReplies); // Decode JSON returned by getNewsArticleFromID function
                if ($newsArticleCommentsReplies != "[]") {
                    for ($j = 0; $j < sizeof($newsArticleCommentsRepliesJSON); $j++) {
                        displayReply($newsArticleCommentsRepliesJSON, $j, true, $iterator);
                        replyForm(true, $iterator, $newsArticleCommentsRepliesJSON, $j);
                        $iterator++;
                    }
                }
            }
        }
    }
} else {
    noCommentsDisplay();
}

This is how I am displaying one reply to a comment...I am sure that there is a more efficient method of doing this without having to hard code multiple for loops.