I am building comments for CMS. I have the following data structure:
$comments=[
[comment_id=>1,...,comment_repay_to=>null],
[comment_id=>2,...,comment_repay_to=>1],
[comment_id=>3,...,comment_repay_to=>2],
[comment_id=>4,...,comment_repay_to=>null]
];
I want to turn into:
$comments=[
[
'comment'=>[comment_id=>1,...,comment_repay_to=>null],
'children'=>[
[
'comment'=>[comment_id=>2,...,comment_repay_to=>1],
'children'=>[...]
],
],
'comment'=>[comment_id=>4,...,comment_repay_to=>null],
],
];
I have written the following incomplete code. I am stuck in loops.
$arr=[];
$level=0;
foreach ($comments as $comment){
if($comment->comment_replay_to==null){
$temp_comment=$comment;
$temp_arr=[
'comment'=>$comment,
'children'=>[]
];
$level++;
while($level>0){
}
}
}
Any help would be highly appreciated. Thanks.
Edit: I got the answer on this link:https://books.google.com.pk/books?id=xHc5DwAAQBAJ&pg=PA109&lpg=PA109&dq=recursive+display+threaded+comments&source=bl&ots=b2PLCOnMnX&sig=ACfU3U1Jy6iUh-qeOZlPfgz3QnIAFlitLA&hl=en&sa=X&ved=2ahUKEwjK86Sm6PjjAhXsA2MBHfORAdsQ6AEwEnoECAkQAQ#v=onepage&q&f=false