I have looked at merging recursive in inner arrays but the is not going so well.
I am trying to get the array below to make a multidimensional array by nesting comments with comment_ID that is equal to comment_parent.
Desired result: Each comment with the comment_id == comment_parent should nest inside.
<?php $comments = array( array( 'comment_ID' => '4', 'comment_post_ID' => '1', 'comment_author' => 'cc', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'http://localhost:10004', 'comment_author_IP' => '127.0.0.1', 'comment_date' => '2021-01-08 15 =>59 =>28', 'comment_date_gmt' => '2021-01-08 15 =>59 =>28', 'comment_content' => 'Surely I am the second level comment in queue.', 'comment_approved' => '1', 'comment_parent' => '1', 'comment_karma' => '0', 'comment_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv =>84.0) Gecko/20100101 Firefox/84.0', 'comment_type' => 'comment', 'user_id' => '1' ), array( 'comment_ID' => '3', 'comment_post_ID' => '1', 'comment_author' => 'cc', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'http://localhost:10004', 'comment_author_IP' => '127.0.0.1', 'comment_date' => '2021-01-08 15 =>58 =>38', 'comment_date_gmt' => '2021-01-08 15 =>58 =>38', 'comment_content' => 'I am third level comment', 'comment_approved' => '1', 'comment_parent' => '2', 'comment_karma' => '0', 'comment_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv =>84.0) Gecko/20100101 Firefox/84.0', 'comment_type' => 'comment', 'user_id' => '1' ), array( 'comment_ID' => '2', 'comment_post_ID' => '1', 'comment_author' => 'cc', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'http://localhost:10004', 'comment_author_IP' => '127.0.0.1', 'comment_date' => '2021-01-08 15 =>49 =>52', 'comment_date_gmt' => '2021-01-08 15 =>49 =>52', 'comment_content' => 'I am a second level comment for the first one. Hello world comment.', 'comment_approved' => '1', 'comment_parent' => '1', 'comment_karma' => '0', 'comment_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv =>84.0) Gecko/20100101 Firefox/84.0', 'comment_type' => 'comment', 'user_id' => '1' ), array( 'comment_ID' => '1', 'comment_post_ID' => '1', 'comment_author' => 'A WordPress Commenter', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'https =>//wordpress.org/', 'comment_author_IP' => '', 'comment_date' => '2021-01-02 13 =>03 =>52', 'comment_date_gmt' => '2021-01-02 13 =>03 =>52', 'comment_content' => 'Update, Hi, this is a comment.\r\nTo get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.\r\nCommenter avatars come from <a href=\'https =>//gravatar.com\' rel=\'nofollow ugc\'>Gravatar</a>.', 'comment_approved' => '1', 'comment_parent' => '0', 'comment_karma' => '0', 'comment_agent' => '', 'comment_type' => 'comment', 'user_id' => '0' ) ); $grouped = []; // $arr is your initial array array_walk($comments, function($v) use (&$grouped){ if (array_key_exists($v['comment_ID'], $grouped)) { $grouped[$v['comment_ID']]['comment_parent'][] = $v['comment_parent']; } else { $v['comment_parent'] = [$v['comment_parent']]; $grouped[$v['comment_ID']] = $v; } }); return $grouped; Desired result: Each comment with the comment_id == comment_parent should nest inside.
I need to have comments { comment 1 { Reply to comment one { Reply to comment of comment one } } comment 2 { Reply to comment two { Reply to comment of comment two } } }
a reply simply has the same parent ID as the comment ID.
<?php $result = array( array( // Comments with key comment_post_ID value 1 or 2 ... level one '1' => array( // comments with key comment_parent value 1 for example next level '1' => array( array( // details inset. 'comment_ID' => '4', 'comment_author' => 'cc', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'http://localhost:10004', 'comment_author_IP' => '127.0.0.1', 'comment_date' => '2021-01-08 15 =>59 =>28', 'comment_date_gmt' => '2021-01-08 15 =>59 =>28', 'comment_content' => 'Surely I am the second level comment in queue.', 'comment_approved' => '1', 'comment_type' => 'comment', 'user_id' => '1' ), array( // details inset. 'comment_ID' => '3', 'comment_author' => 'cc', 'comment_author_email' => '[email protected]', 'comment_author_url' => 'http://localhost:10004', 'comment_author_IP' => '127.0.0.1', 'comment_date' => '2021-01-08 15 =>59 =>28', 'comment_date_gmt' => '2021-01-08 15 =>59 =>28', 'comment_content' => 'Surely I am the second level comment in queue.', 'comment_approved' => '1', 'comment_type' => 'comment', 'user_id' => '1' ), ) ) ) );