<?php

	$comments = [
		'2020-01-08 20:40:00'=>[
			id=>1,
			parent_id=>0,
			comment_text=>'1',
			date_posted=>'2020-01-08 20:40:00',
		],
		'2020-01-08 20:41:00'=>[
			id=>2,
			parent_id=>1,
			comment_text=>'1.1',
			date_posted=>'2020-01-08 20:41:00',
		],
		'2020-01-08 20:42:00'=>[
			id=>3,
			parent_id=>0,
			comment_text=>'2',
			date_posted=>'2020-01-08 20:42:00',
		],
		'2020-01-08 20:43:00'=>[
			id=>4,
			parent_id=>0,
			comment_text=>'3',
			date_posted=>'2020-01-08 20:43:00',
		],
		'2020-01-08 20:44:00'=>[
			id=>5,
			parent_id=>3,
			comment_text=>'2.1',
			date_posted=>'2020-01-08 20:44:00',
		],
		'2020-01-08 20:45:00'=>[
			id=>6,
			parent_id=>2,
			comment_text=>'1.1.1',
			date_posted=>'2020-01-08 20:45:00',
		],
		'2020-01-08 20:46:00'=>[
			id=>7,
			parent_id=>1,
			comment_text=>'1.2',
			date_posted=>'2020-01-08 20:46:00',
		],
	];
	krsort($comments);
    $comment_hash = [];

    foreach($comments as $comment) {
        if(!$comment_hash[$comment['parent_id']]) {
            $comment_hash[$comment['parent_id']] = [];
        }

        $comment_hash[$comment['parent_id']][] = $comment;
    }

    function displayComments($comments, $comment_hash, $depth) {
        foreach($comments as $comment) {
			print(str_repeat(' ', 4 * $depth));
            print($comment['comment_text']);
            print("\n");

            displayComments($comment_hash[$comment['id']], $comment_hash, $depth + 1);
        }
    }

    displayComments($comment_hash[0], $comment_hash, 0);