Displaying comments and replies retrieved from a database I created an example (for me) of how I must get the comments and replies from the database.

Here is the result for example:

//I am ONLY thinking a query like this
$q = "SELECT * FROM comments_table WHERE post = 'url_code' ORDER BY date DESC";

//An example result (Not used the query above)

$comment = array(
            array(
                'id'            =&gt; '7',
                'user'          =&gt; 'Jul',
                'post_id'       =&gt; '1',
                'comment_id'    =&gt; '7',
                'reply_id'      =&gt; '1',
                'date'          =&gt; 'Jul 2016',
                'message'       =&gt; 'hello7'
            ),
            array(
                'id'            =&gt; '6',
                'user'          =&gt; 'Jun',
                'post_id'       =&gt; '1',
                'comment_id'    =&gt; '6',
                'reply_id'      =&gt; '1',
                'date'          =&gt; 'Jun 2016',
                'message'       =&gt; 'hello6'
            ),
            array(
                'id'            =&gt; '5',
                'user'          =&gt; 'May',
                'post_id'       =&gt; '1',
                'comment_id'    =&gt; '5',
                'reply_id'      =&gt; '3',
                'date'          =&gt; 'May 2016',
                'message'       =&gt; 'hello5'
            ),
            array(
                'id'            =&gt; '4',
                'user'          =&gt; 'Apr',
                'post_id'       =&gt; '1',
                'comment_id'    =&gt; '4',
                'reply_id'      =&gt; '2',
                'date'          =&gt; 'Apr 2016',
                'message'       =&gt; 'hello4'
            ),
            array(
                'id'            =&gt; '3',
                'user'          =&gt; 'Mar',
                'post_id'       =&gt; '1',
                'comment_id'    =&gt; '3',
                'reply_id'      =&gt; '',
                'date'          =&gt; 'Mar 2016',
                'message'       =&gt; 'hello3'
            ),
            array(
                'id'            =&gt; '2',
                'user'          =&gt; 'Feb',
                'post_id'       =&gt; '1',
                'comment_id'    =&gt; '2',
                'reply_id'      =&gt; '',
                'date'          =&gt; 'Feb 2016',
                'message'       =&gt; 'hello2'
            ),
            array(
                'id'            =&gt; '1',
                'user'          =&gt; 'Jan',
                'post_id'       =&gt; '1',
                'comment_id'    =&gt; '1',
                'reply_id'      =&gt; '',
                'date'          =&gt; 'Jan 2016',
                'message'       =&gt; 'hello'
            )
        );


Then the code to output it in the page:

foreach ($comment as $key =&gt; $value) {
    if(empty($value['reply_id'])) {
        $comments[$value['comment_id']] = array('user' =&gt; $value['user'], 'message' =&gt; $value['message']);
    } else {
        $replies[] = array('id' =&gt; $value['id'], 'reply_id' =&gt; $value['reply_id'], 'user' =&gt; $value['user'], 'message' =&gt; $value['message']);
    }
}

$reply_count = count($replies);
for ($i=0; $i &lt; $reply_count; $i++) {
    $reply_id = $replies[$i]['reply_id'];
    if(isset($comments[$reply_id])) {
        $cc[$reply_id]['comment'] = array('user' =&gt; $comments[$reply_id]['user'], 'message' =&gt; $comments[$reply_id]['message']);
        $cc[$reply_id][$replies[$i]['id']] = array('user' =&gt; $replies[$i]['user'], 'message' =&gt; $replies[$i]['message']);
    }
}

foreach ($cc as $kkk =&gt; $vvv) {
    $reply = '';
    foreach ($vvv as $kk =&gt; $vv) {
        if(is_int($kk)) {
            $reply .= $vv['user'] . ' reply: ' . $vv['message'] . '&lt;br&gt;';
        } else {
            $comment = $vv['user'] . ' comment: ' . $vv['message'] . '&lt;br&gt;';
        }
    }
    echo $comment . $reply . '&lt;br&gt;';
}


The results are okay, but my question is, is this code okay to use?
It seems you wish to batch the posted messages together into threads.  In other words, create a "parent - children" relationship.  This can be achieved simply by using one loop to generate the new data structure and one smaller loop (I use array_map()) to display the text.

Code: (Demo)

foreach ($comments as $data) {
    if ($data['reply_id']!='') {
        $threads[$data['reply_id']][]="{$data['user']} reply: {$data['message']}";   // append element
    } else {
        if (!isset($threads[$data['id']])) {  // prevent Notice
            $threads[$data['id']] = [];
        }
        array_unshift($threads[$data['id']], "{$data['user']} comment: {$data['message']}");  // prepend element
    }
}

// display data (with no unwanted line breaks):
echo implode("&lt;br&gt;&lt;br&gt;", array_map(function($v){return implode("&lt;br&gt;",$v);}, $threads));


Output (same as your posted code provides):

Jan comment: hello
Jul reply: hello7
Jun reply: hello6

Mar comment: hello3
May reply: hello5

Feb comment: hello2
Apr reply: hello4


Some additional notes:


Using ORDER BY date DESC will place newer replies above older replies, be sure that this makes sense in terms of discussion "flow".
There appears to be data redundancy with id and comment_id.  If there is a difference between these two columns of data that doesn't present itself in your posted task, then they both can stay, otherwise you should omit one of these columns from your database.
A final consideration... when dealing with thread-based messages/posts/data, it simplifies tasks like these to assign a thread_id, then you can GROUP BY and/or ORDER BY the column in your SQL and spare much of the php handling.