做一个无限级评论,想让3级以后的统一归到2级里面显示

浏览:346 发布日期:2017/12/13 分类:求助交流
目前使用的这个.    public static function unlimitForLayer($data, $pid = 0, $name = 'child')
    {
        $arr = [];
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $v[$name] = self::unlimitForLayer($data, $v['id'], $name);
                $arr[] = $v;
            }
        }
        return $arr;
    }
生成后的代码:Array
(
    [0] => Array
        (
            [id] => 17
            [pid] => 0
            [name] => AAAAAAAAAAAAAAAAAAAAA
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 18
                            [pid] => 17
                            [name] => BBBBBBBBBBBBBBBBBB
                            [child] => Array
                                (
                                )
                        )

                    [1] => Array
                        (
                            [id] => 13
                            [pid] => 17
                            [name] => CCCCCCCCCCCCCCCC
                            [child] => Array
                                (
                                )
                        )

                    [2] => Array
                        (
                            [id] => 11
                            [pid] => 17
                            [name] => DDDDDDDDDDDDDDDDDDDDDDDDDDD
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 9
                                            [pid] => 11
                                            [name] => EEEEEEEEEEEEEEEEEEEEEEEE
                                            [child] => Array
                                                (
                                                )
                                        )
                                )
                        )
                )
        )

    [1] => Array
        (
            [id] => 16
            [pid] => 0
            [name] => ABABAABABAABABAAB
            [child] => Array
                (
                )
        )

    [2] => Array
        (
            [id] => 1
            [pid] => 0
            [name] => QEWERRRRQEWERRRR
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 8
                            [pid] => 1
                            [name] => ZXCVZVZVZXCVZVZVZXCVZVZV
                            [child] => Array
                                (
                                )
                        )
                )
        )
)
我想要的结果是:Array
(
    [0] => Array
        (
            [id] => 17
            [pid] => 0
            [name] => AAAAAAAAAAAAAAAAAAAAA
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 18
                            [pid] => 17
                            [name] => BBBBBBBBBBBBBBBBBB
                            [child] => Array
                                (
                                )
                        )

                    [1] => Array
                        (
                            [id] => 13
                            [pid] => 17
                            [name] => CCCCCCCCCCCCCCCC
                            [child] => Array
                                (
                                )
                        )

                    [2] => Array
                        (
                            [id] => 11
                            [pid] => 17
                            [name] => DDDDDDDDDDDDDDDDDDDDDDDDDDD
                            [child] => Array
                                (
                                )
                        )
                        
                    [3] => Array
                        (
                            [id] => 11
                            [pid] => 17
                            [name] => EEEEEEEEEEEEEEEEEEEEEEEE,这项或者更多的下级都弄到2级里面
                            [child] => Array
                                (
                                )
                        )
                )
        )

    [1] => Array
        (
            [id] => 16
            [pid] => 0
            [name] => ABABAABABAABABAAB
            [child] => Array
                (
                )
        )

    [2] => Array
        (
            [id] => 1
            [pid] => 0
            [name] => QEWERRRRQEWERRRR
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 8
                            [pid] => 1
                            [name] => ZXCVZVZVZXCVZVZVZXCVZVZV
                            [child] => Array
                                (
                                )
                        )
                )
        )
)
最佳答案
评论( 相关
后面还有条评论,点击查看>>