Sunday, March 19, 2017

There is a issue of recursion

I made many attempts, but still don't know where I went wrong

    public static function foreachComments($comments, $arr_id = array(0), $arr_comments = [])
{
    $arr = $comments;
    $s_comments = &$arr;
    if (count($arr)!==0) {
        foreach ($comments as $key => $value) {
            $a = $value->father_id;
            $b = $arr_id[count($arr_id) - 1];
            if ($a == $b) {
                $arr_comments[count($arr_comments)] = $comments[$key];
                $arr_id[count($arr_id)] = $value->id;
                $s_comments = array_except($comments, $key);
                self::foreachComments($s_comments, $arr_id, $arr_comments);
            }

        }
    } else {
        return $arr_comments;
    }

    $arr_id = array_except($arr_id, count($arr_id) - 1);
    self::foreachComments($s_comments, $arr_id, $arr_comments);
}

If the code is written, this variable in would be an error: Undefined offset: 7

If the return $arr_comments; modified into the dd ($arr_comments), will correct output data what I want


This is my data:

DATA PIC


INSERT INTO blog.blog_comment (id, comment_name, comment_email, comment_content, art_id, created_at, updated_at, father_id) VALUES ('1', '林', '527040@qq.com', '帅帅帅帅帅帅帅,博主大帅比', '2', '1489855142', '1489855142', '0');

INSERT INTO blog.blog_comment (id, comment_name, comment_email, comment_content, art_id, created_at, updated_at, father_id) VALUES ('2', '张三', '213135@qq.com', '我就试试再来一条是什么感觉', '2', '1489936387', '1489936387', '11');

INSERT INTO blog.blog_comment (id, comment_name, comment_email, comment_content, art_id, created_at, updated_at, father_id) VALUES ('10', '威尔斯密斯王五', '52040@qq.com', '阿斯大苏打方法', '2', '1489855142', '1489855142', '9');

INSERT INTO blog.blog_comment (id, comment_name, comment_email, comment_content, art_id, created_at, updated_at, father_id) VALUES ('4', '习大大', '123@qq.com', '我再试试时间对不对', '2', '1489936387', '1489936387', '0');

INSERT INTO blog.blog_comment (id, comment_name, comment_email, comment_content, art_id, created_at, updated_at, father_id) VALUES ('9', '穆奇', '5290540@qq.com', '阿斯大苏打方法', '2', '1489855142', '1489855142', '0');

INSERT INTO blog.blog_comment (id, comment_name, comment_email, comment_content, art_id, created_at, updated_at, father_id) VALUES ('11', '尼古拉斯凯奇', '213135@qq.com', '我底下的子评论展示', '2', '1489936387', '1489936387', '1');

INSERT INTO blog.blog_comment (id, comment_name, comment_email, comment_content, art_id, created_at, updated_at, father_id) VALUES ('12', '尼古拉斯赵四', '213135@qq.com', '对对对,说得对!', '2', '1489855142', '1489855142', '1');

INSERT INTO blog.blog_comment (id, comment_name, comment_email, comment_content, art_id, created_at, updated_at, father_id) VALUES ('15', '威尔斯密斯', '529050@qq.com', '1231231', '2', '1489936387', '1489936387', '2');

INSERT INTO blog.blog_comment (id, comment_name, comment_email, comment_content, art_id, created_at, updated_at, father_id) VALUES ('16', '栖木', '52900@qq.com', '这是第四篇的评论', '4', '1489938029', '1489938029', '0');



via MonKing

Advertisement