如何在php中将数据从一个数组附加到另一个数组

如何在php中将数据从一个数组附加到另一个数组,php,arrays,laravel,for-loop,Php,Arrays,Laravel,For Loop,我正在构建一个[post,comments]项目,其中post详细信息存储在posts表中,而评论详细信息存储在comments表中。该项目是用Laravel(Php)生成的。这是一段代码 //get data from Post table $posts = $this->postsRepo->getAllPosts(); //get data from comments table $comments = $this->commentRepo->getAllComme

我正在构建一个[post,comments]项目,其中post详细信息存储在posts表中,而评论详细信息存储在comments表中。该项目是用Laravel(Php)生成的。这是一段代码

//get data from Post table
$posts = $this->postsRepo->getAllPosts();
//get data from comments table
$comments = $this->commentRepo->getAllComments();
print_r($posts);
print_r($comments);
post表的结果

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [post_text] => This is a test post
        )
)
    [1] => stdClass Object
        (
            [id] => 2
            [post_text] => This is another test
        )
)
注释表的结果

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [post_id] => 1
            [comments_text] => This is a test comment 1
        )
    [1] => stdClass Object
        (
            [id] => 2
            [post_id] => 1
            [comments_text] => This is a test comment 2
        )
)
现在我想要这样的结果

Array
(
    [0] => stdClass Object
    (
        [id] => 1
        [post_title] => This is a test post         
        [comments] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => 1
                        [post_id] => 1
                        [comment_text] => This is a test comment 1
                    )

                [1] => stdClass Object
                    (
                        [id] => 2
                        [post_id] => 1
                        [comment_text] => This is a test comment 2 
                    )
            )
        )
    )
    [1] => stdClass Object
    (
        [id] => 2
        [post_title] => This is another test
        [comments] => Array()
    )

)
我使用了下面的技术,它是有效的

foreach ($posts as $postIndex => $post) {
    foreach ($comments as $commentIndex => $comment) {
        if($post->id == $comment->post_id) {
            $posts[$postIndex]->comments[] = $comment;
        }
    }
}
还有另一种方法,比如在postfor循环中运行注释。我将把post_id发送到comments表。但随着数据的增加,这项技术将变得缓慢

有谁能推荐一种更好的技术来解决这个问题吗?
另外,我使用了存储库设计模式来获取帖子和评论

您最好使用有说服力的关系。。。 但为了回答当前案例中的问题,如果您需要用php的方式来完成这项工作

// for all the post of the posts array
    foreach ($posts as $key => $value) {

        // get comments which only belongs to the post
        $post_comments = [];
        foreach ($comments as $comment) {

            if ($comment['post_id'] == $value['id']) {
                array_push($post_comments, $comment);
            }
        }

        // add a new attribute (comments) to each post
        // set created comments array of the post
        $post[$key]['comments'] = $post_comments;
    }

有几种方法可以做到这一点,具体取决于您使用的PHP版本(尽管1将始终有效)。主要原则是通过ID对其中一个数组进行索引,这样就可以使用索引来设置值,而不是在两个数组上循环。由于每篇文章都可以有多条评论,因此似乎更容易通过ID为文章编制索引,并在评论上循环,将它们添加到正确的文章中

$posts = [
    (object)[
        "id" => 1,
        "post_text" => "This is a test post"
    ],
    (object)[
        "id" => 2,
        "post_text" => "This is another test"
    ]
];

$comments = [(object)
    [
        "id" => 1,
        "post_id" => 1,
        "comments_text" => "This is a test comment 1"
        ],
    (object)
    [
        "id" => 2,
        "post_id" => 1,
        "comments_text" => "This is a test comment 2"
        ]
    ];

$postIndex = array_column($posts, null, "id");   
//$postIndex = array_reduce($posts, function ($result, $post) { // PHP pre 7
//    $result[$post->id] = $post;
//    return $result;
//}, array());
foreach ( $comments as $comment )   {
    $postIndex[ $comment->post_id ]->comments[] = $comment;
}
echo "<pre>";
print_r($postIndex);

唯一的区别是,与PHP7.0相比,您可以将
array\u column()
用于对象数组。在此之前,您必须手动执行转换。

如果您循环帖子,则可以使用array\u intersect和array\u intersect\u键。
这意味着您只能循环一次一个数组,但要使用数组函数获得正确的注释。
这意味着数组必须是数组而不是对象


为什么不使用laravel关系?在Post模型中定义关系并给出名称注释更改查询PostModel::with('comments')->get();我不是在用雄辩。使用简单的数据库查询您使用的是什么版本的PHP?放弃repo设计模式,使用雄辩的。它也做同样的事情,并且已经有代码可以为你做这件事。我认为你的注释id 2在错误的地方。我相信评论数组中的“post_id”表示它与哪个帖子相关to@Andreas,谢谢-我错了。。。仅测试是否有人在观看>;)我还有两个表(帖子喜欢,评论喜欢)。要将文章连接到类似于Posts数组(类似于我们连接的评论)&类似于comments数组的评论附加了一个表。现在,表结构将如下所示:用户->帖子->评论。这个用什么?请帮帮我,我明天才有时间解决这个问题。您最好使用到目前为止的代码和您想要实现的目标提出一个新问题。
Array
(
    [1] => stdClass Object
        (
            [id] => 1
            [post_text] => This is a test post
            [comments] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 1
                            [post_id] => 1
                            [comments_text] => This is a test comment 1
                        )

                    [1] => stdClass Object
                        (
                            [id] => 2
                            [post_id] => 1
                            [comments_text] => This is a test comment 2
                        )

                )

        )

    [2] => stdClass Object
        (
            [id] => 2
            [post_text] => This is another test
        )

)
Foreach($posts as $key => $post){
    $matches = array_intersect(array_column($comments, "post_id"), [$post["id"]]);
    $new[$key][] = $post;
    $new[$key]["comments"][] = array_intersect_key($comments, $matches);
}

Var_dump($new);