dongzhi4690 2015-04-16 23:00
浏览 48
已采纳

使用递归函数的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

  • 写回答

1条回答

  • 普通网友 2015-04-16 23:16
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示