使用递归函数的PHP foreach多维数组[关闭]

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

douruduan8812
douruduan8812 写一个mcve:stackoverflow.com/help/mcve
5 年多之前 回复
dqybjj3497
dqybjj3497 我也发布了代码。
5 年多之前 回复
doutan5724
doutan5724 嗨,您需要向我们展示一些您尝试过的代码,并具体说明您遇到的问题。目前,这似乎是要求其他人为您编写代码,这不是本网站的目的。
5 年多之前 回复
duandian2725
duandian2725 并发布代码,而不仅仅是数据结构
5 年多之前 回复
douxian9943
douxian9943 在此处发布您的代码,而不是图像。
5 年多之前 回复

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.

dthl8036
dthl8036 感谢您的建议和理解我的问题!
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐