将两个json数组合并为一个

问题描述 投票:2回答:3

尝试将两个JSON响应合并为一个,但问题是数据显示在两个数组中,我希望它在一个数组中。我如何在Lumen / Laravel中实现它

尝试收集两个阵列或响应

 public function index(Request $request)
    {
        $post = Post::orderBy('id', 'DESC')->get();
        $post_images = PostImage::orderBy('id', 'DESC')->get();
        return $this->successResponse($posts.$post_image );        
    }

预期:-

{
    "post": {
        "id": 14,
        "post_id": 798965728,
        "user_id": 1,
        "location": "first",
        "title": "un8852",
        "cooked_time": "1554329910",
        "dispose_time": "1554373110",
        "food_type": "nv",
        "description": "asdfg",
        "serve_quantity": 23,
        "lat": 19.08,
        "lon": 73,    
        "id": 10,
        "post_id": 798965728,
        "image_name1": null,
        "image_name2": null,
        "image_name3": null,
        "image_name4": null,
        "image_name5": null,
    },
    "st": "1",
    "msg": "success"
}

拿到:-

{
   "post":"{\"id\":14,\"post_id\":798965728,\"user_id\":1,\"location\":\"first\",\"title\":\"un8852\",\"cooked_time\":\"1554329910\",\"dispose_time\":\"1554373110\",\"food_type\":\"nv\",\"description\":\"asdfg\",\"serve_quantity\":23,\"lat\":19.08,\"lon\":73,\"created_at\":\"2019-04-04 10:20:18\",\"updated_at\":\"2019-04-04 10:20:18\"}{\"id\":10,\"post_id\":798965728,\"image_name1\":null,\"image_name2\":null,\"image_name3\":null,\"image_name4\":null,\"image_name5\":null,\"created_at\":\"2019-04-04 10:20:18\",\"updated_at\":\"2019-04-04 10:20:18\"}",
   "st":"1",
   "msg":"success"
}
php laravel laravel-5.2 lumen lumen-5.4
3个回答
1
投票

那里有一些缺失的部分,但我想我看到发生了什么。

根据你得到的结果,在这段代码中看起来像$posts$post_image是雄辩的模型。

return $this->successResponse($posts.$post_image ); 

当你连接它们时,它们的__toString()方法将它们转换为字符串,这是使用toJson()方法完成的。所以基本上你有两个JSON对象粘在一起,这是无效的JSON,然后successResponse()方法再次编码它们。

要合并它们,您可以将它们转换为数组,合并它们,然后将结果传递给successResponse()

$merged = array_merge($posts->toArray(), $post_image->toArray());
return $this->successResponse($merged);

但是你想要的结果是不可能的。 “post”对象有两个不同的“id”值。你只能得到一个。如果你使用

$merged = array_merge($posts->toArray(), $post_image->toArray());

然后第二个对象的id值将替换第一个。如果要保留第一个id值,则需要使用union而不是array_merge

$merged = $a->toArray() + $b->toArray(); 

0
投票

你绝对可以连接两个JSON数组,你必须解析对象并连接它们并重新进行字符串化。

这个问题也可以在这里得到解答。 https://stackoverflow.com/a/10384890/1684254


0
投票

我认为你不能将2个JSON对象连接成字符串。

正确的方法是:

  1. 获取Post和PostImage对象 $ post = Post :: orderBy('id','DESC') - > get(); $ post_images = PostImage :: orderBy('id','DESC') - > get();
  2. 序列化Post对象https://laravel.com/docs/5.8/eloquent-serialization
  3. 使用下面描述的方法将PostImage对象的每个字段(image_name1 .. image_name5)添加到JSON How to add attribute in JSON in PHP?
  4. 返回JSON

更新:

Post :: orderBy('id','DESC') - > get() - > first()返回一个对象。

Post :: orderBy('id','DESC') - > get()返回一个对象集合,它需要一个不同的方法。

试试这个:

$post = Post::orderBy('id', 'DESC')->get();

$post->map(function ($item, $key) {
    $post_images = PostImage::where('post_id', $item->id)->get()->first();
    $item->setAttribute('image_name1',$post_images->image_name1);
    $item->setAttribute('image_name2',$post_images->image_name2);
    $item->setAttribute('image_name3',$post_images->image_name3);
    $item->setAttribute('image_name4',$post_images->image_name4);
    $item->setAttribute('image_name5',$post_images->image_name5);
    return $item;
});

return $this->successResponse($posts);
© www.soinside.com 2019 - 2024. All rights reserved.