-1

I am using a recursive function to create a comment/reply system as yt or fb is using. I've done the function, but I cannot figure it out how to make the foreach function if I don't know for sure how many branches the array will have.

$coments = $this->mu_model->getByWhereStmt('comments', 'article_id', $article->id);
$comments = array();
    $comm = array();
    foreach($coments as $coment) {
        if($coment->reply_to_msg_id == 0) {
            $comm = array(
                'id' => $coment->id,
                'sender_id' => $coment->sender_id,
                'message' => $coment->message,
                'article_id' => $coment->article_id,
                'like' => $coment->like,
                'reply_to' => $coment->reply_to_msg_id,
                'added_date' => $coment->added_date,
                'deleted' => $coment->deleted,
                );
            $reply_id = $coment->id;
            if(!empty(reply_msg($reply_id, $coments))) {
                $repliess = reply_msg($reply_id, $coments);
                array_push($comm, $repliess);
            }
            array_push($comments, $comm);
        }
    }

function reply_msg($reply_id, $coments) {
        $replies = array();
        $reply = array();
        foreach($coments as $r_comment) {
            if($r_comment->reply_to_msg_id != 0) {
                if($r_comment->reply_to_msg_id == $reply_id) {  
                    $reply = array(
                            'id' => $r_comment->id,
                            'sender_id' => $r_comment->sender_id,
                            'message' => $r_comment->message,
                            'article_id' => $r_comment->article_id,
                            'like' => $r_comment->like,
                            'reply_to' => $r_comment->reply_to_msg_id,
                            'added_date' => $r_comment->added_date,
                            'deleted' => $r_comment->deleted,
                        );
                    if(!empty(reply_msg($r_comment->id, $coments))) {
                        $repli = reply_msg($r_comment->id, $coments);
                        array_push($reply, $repli);
                    }
                    array_push($replies, $reply);
                }
            }
        }
        return $replies;

Here is an example: http://i58.tinypic.com/118f8tt.png

5
  • 6
    Post your code here, not as an image. Commented Apr 16, 2015 at 23:05
  • and post the code aswell, not just the data structure Commented Apr 16, 2015 at 23:10
  • Hi, you need to show us some code that you've tried, and explain specifically where you got stuck. At the moment, this appears to be a request for other people to write your code for you, which is not the purpose of this site. Commented Apr 16, 2015 at 23:19
  • I posted the code as well. Commented Apr 16, 2015 at 23:21
  • Write a mcve: stackoverflow.com/help/mcve Commented Apr 17, 2015 at 0:29

1 Answer 1

1

What about:

function process($array) {
    foreach ($array as $key=>$value) {
        // Process the content of the array
    }
    if (isset($array[8])) // If we have linked arrays
        foreach($array[8] as $subArray)
            process($subArray);
}

PS: I would also set a key for the linked arrays, like 'children' or 'subcomments'. In this way you could call isset($array['children']) and so on, which is more maintainable than accesing the array by its index.

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

1 Comment

Thank you for your advice and for understanding my question!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.