0

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

1
  • Will parent always come before child in the initial array? Commented Aug 9, 2019 at 15:51

2 Answers 2

1

Not exactly beautiful, but should point you into the right direction:

<?php

// process input in reverse order
$input = array_reverse([
    ['id' => 1, 'message' => 'A', 'parent' => null],
    ['id' => 2, 'message' => 'B', 'parent' => 1],
    ['id' => 3, 'message' => 'C', 'parent' => 2],
    ['id' => 4, 'message' => 'D', 'parent' => null]
]);

// use 'id' as actual key for the entry, saves a lot of hassle
array_walk($input, function($entry) use (&$output) {
    $output[$entry['id']] = $entry;
}); 

array_walk($output, function (&$entry, $id) use (&$output) {
    $parent = $entry['parent'];
    unset($entry['parent']);
    // if entry belongs into a parent, then move it there
    if (array_key_exists($parent, $output)) {
        $output[$parent]['children'][] = $entry;
        unset($output[$id]);
    }
});

print_r($output);

The output is:

Array
(
    [4] => Array
        (
            [id] => 4
            [message] => D
        )

    [1] => Array
        (
            [id] => 1
            [message] => A
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [message] => B
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [message] => C
                                        )

                                )

                        )

                )

        )

)
Sign up to request clarification or add additional context in comments.

5 Comments

What a sphinx line comment... Would you care to elaborate why it is "not exactly working" and what you actual situation is? ;-)
I didn't got the desired out, as you mentioned in the answer.
You mean the code above gave another output in your case? Are you sure you didn't alter it?
No I didn't alter it. Thanks anyways.
Then I fail to understand what you mean by your last comment. The output created by that code is exactly the desired output, according to your question.
0

This is the final code for someone's help

public static function commentRecursive($comments,$n){
        if(isset($comments[$n])){
            self::$str.="<ul>";
            foreach($comments[$n] as $comment){
                self::$str.=$comment['comment_content'];
                self::commentRecursive($comments,$comment['comment_id']);
            }
            self::$str.="</ul>";
        }
    }
    public static function commentsDataStructure($comments){
        self::$str='';
        $output=[];
        foreach ($comments as $comment){
            $output[$comment['comment_reply_to']][]=$comment;
        }
        self::commentRecursive($output,0);
        return self::$str;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.