如何获取每个分类下的前10篇文章

浏览:387 发布日期:2021/04/29 分类:ThinkPHP6专区 关键字: 数组
在做一个文章系统的时候首页需要获取各个分类下的前10篇文章
单分类
我是先读取各分类id,建立一个数组
然后再循环一次,读取各个分类下的文章,
这样一看效率就不高,能否一次或者两次读取生成数组呢。
另外最后生成的数组最好类似这样Array
(
    [0] => Array
        (
            [id] => 1
            [name] => 资讯
            [article] => think\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 2
                                    [title] => 文章2
                                )
                            [1] => Array
                                (
                                    [id] => 1
                                    [title] => 文章1
                                )
                        )
                )
        )

    [1] => Array
        (
            [id] => 2
            [name] => 教程
            [article] => think\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 3
                                    [title] => 文章3
                                )

                            [1] => Array
                                (
                                    [id] => 4
                                    [title] => 文章4
                                )
                        )
                )
        )

    [2] => Array
        (
            [id] => 3
            [name] => 评测
            [article] => think\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 5
                                    [title] => 文章5
                                )
                        )
                )
        )
)
最佳答案
评论( 相关
后面还有条评论,点击查看>>