fork download
  1. <?php
  2.  
  3. $comments = [
  4. '2020-01-08 20:40:00'=>[
  5. id=>1,
  6. parent_id=>0,
  7. comment_text=>'1',
  8. date_posted=>'2020-01-08 20:40:00',
  9. ],
  10. '2020-01-08 20:41:00'=>[
  11. id=>2,
  12. parent_id=>1,
  13. comment_text=>'1.1',
  14. date_posted=>'2020-01-08 20:41:00',
  15. ],
  16. '2020-01-08 20:42:00'=>[
  17. id=>3,
  18. parent_id=>0,
  19. comment_text=>'2',
  20. date_posted=>'2020-01-08 20:42:00',
  21. ],
  22. '2020-01-08 20:43:00'=>[
  23. id=>4,
  24. parent_id=>0,
  25. comment_text=>'3',
  26. date_posted=>'2020-01-08 20:43:00',
  27. ],
  28. '2020-01-08 20:44:00'=>[
  29. id=>5,
  30. parent_id=>3,
  31. comment_text=>'2.1',
  32. date_posted=>'2020-01-08 20:44:00',
  33. ],
  34. '2020-01-08 20:45:00'=>[
  35. id=>6,
  36. parent_id=>2,
  37. comment_text=>'1.1.1',
  38. date_posted=>'2020-01-08 20:45:00',
  39. ],
  40. '2020-01-08 20:46:00'=>[
  41. id=>7,
  42. parent_id=>1,
  43. comment_text=>'1.2',
  44. date_posted=>'2020-01-08 20:46:00',
  45. ],
  46. ];
  47. krsort($comments);
  48. $comment_hash = [];
  49.  
  50. foreach($comments as $comment) {
  51. if(!$comment_hash[$comment['parent_id']]) {
  52. $comment_hash[$comment['parent_id']] = [];
  53. }
  54.  
  55. $comment_hash[$comment['parent_id']][] = $comment;
  56. }
  57.  
  58. function displayComments($comments, $comment_hash, $depth) {
  59. foreach($comments as $comment) {
  60. print(str_repeat(' ', 4 * $depth));
  61. print($comment['comment_text']);
  62. print("\n");
  63.  
  64. displayComments($comment_hash[$comment['id']], $comment_hash, $depth + 1);
  65. }
  66. }
  67.  
  68. displayComments($comment_hash[0], $comment_hash, 0);
Success #stdin #stdout #stderr 0.02s 24660KB
stdin
Standard input is empty
stdout
3
2
    2.1
1
    1.2
    1.1
        1.1.1
stderr
PHP Warning:  Use of undefined constant id - assumed 'id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 5
PHP Warning:  Use of undefined constant parent_id - assumed 'parent_id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 6
PHP Warning:  Use of undefined constant comment_text - assumed 'comment_text' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 7
PHP Warning:  Use of undefined constant date_posted - assumed 'date_posted' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 8
PHP Warning:  Use of undefined constant id - assumed 'id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 11
PHP Warning:  Use of undefined constant parent_id - assumed 'parent_id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 12
PHP Warning:  Use of undefined constant comment_text - assumed 'comment_text' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 13
PHP Warning:  Use of undefined constant date_posted - assumed 'date_posted' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 14
PHP Warning:  Use of undefined constant id - assumed 'id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 17
PHP Warning:  Use of undefined constant parent_id - assumed 'parent_id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 18
PHP Warning:  Use of undefined constant comment_text - assumed 'comment_text' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 19
PHP Warning:  Use of undefined constant date_posted - assumed 'date_posted' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 20
PHP Warning:  Use of undefined constant id - assumed 'id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 23
PHP Warning:  Use of undefined constant parent_id - assumed 'parent_id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 24
PHP Warning:  Use of undefined constant comment_text - assumed 'comment_text' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 25
PHP Warning:  Use of undefined constant date_posted - assumed 'date_posted' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 26
PHP Warning:  Use of undefined constant id - assumed 'id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 29
PHP Warning:  Use of undefined constant parent_id - assumed 'parent_id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 30
PHP Warning:  Use of undefined constant comment_text - assumed 'comment_text' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 31
PHP Warning:  Use of undefined constant date_posted - assumed 'date_posted' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 32
PHP Warning:  Use of undefined constant id - assumed 'id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 35
PHP Warning:  Use of undefined constant parent_id - assumed 'parent_id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 36
PHP Warning:  Use of undefined constant comment_text - assumed 'comment_text' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 37
PHP Warning:  Use of undefined constant date_posted - assumed 'date_posted' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 38
PHP Warning:  Use of undefined constant id - assumed 'id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 41
PHP Warning:  Use of undefined constant parent_id - assumed 'parent_id' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 42
PHP Warning:  Use of undefined constant comment_text - assumed 'comment_text' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 43
PHP Warning:  Use of undefined constant date_posted - assumed 'date_posted' (this will throw an Error in a future version of PHP) in /home/yykYru/prog.php on line 44
PHP Notice:  Undefined offset: 1 in /home/yykYru/prog.php on line 51
PHP Notice:  Undefined offset: 2 in /home/yykYru/prog.php on line 51
PHP Notice:  Undefined offset: 3 in /home/yykYru/prog.php on line 51
PHP Notice:  Undefined offset: 0 in /home/yykYru/prog.php on line 51
PHP Notice:  Undefined offset: 4 in /home/yykYru/prog.php on line 64
PHP Warning:  Invalid argument supplied for foreach() in /home/yykYru/prog.php on line 59
PHP Notice:  Undefined offset: 5 in /home/yykYru/prog.php on line 64
PHP Warning:  Invalid argument supplied for foreach() in /home/yykYru/prog.php on line 59
PHP Notice:  Undefined offset: 7 in /home/yykYru/prog.php on line 64
PHP Warning:  Invalid argument supplied for foreach() in /home/yykYru/prog.php on line 59
PHP Notice:  Undefined offset: 6 in /home/yykYru/prog.php on line 64
PHP Warning:  Invalid argument supplied for foreach() in /home/yykYru/prog.php on line 59