简体   繁体   English

将两个json数组合并为一个

[英]Merge two json array into one

Tried merging two JSON responses into a single one but the problem is the data is displayed in two arrays and I want it in a single array.尝试将两个 JSON 响应合并为一个,但问题是数据显示在两个数组中,我希望它在一个数组中。 How do I achieve it in Lumen/Laravel我如何在 Lumen/Laravel 中实现它

Tried contatinating two arrays or responses尝试包含两个数组或响应

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

Expected:-预期的:-

{
    "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"
}

Got:-得到了:-

{
   "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"
}

There are some missing pieces there, but I think I see what's happening.那里有一些缺失的部分,但我想我明白发生了什么。

Based on the result you're getting, it looks like $posts and $post_image in this code are eloquent models.根据您得到的结果,此代码中的$posts$post_image看起来是雄辩的模型。

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

When you concatenate them, their __toString() methods convert them to strings, which is done using the toJson() method.当您连接它们时,它们的__toString()方法将它们转换为字符串,这是使用toJson()方法完成的。 So basically you have two JSON objects stuck together, which isn't valid JSON, and then the successResponse() method encodes them again.所以基本上你有两个 JSON 对象粘在一起,这不是有效的 JSON,然后successResponse()方法再次对它们进行编码。

To merge them, you can convert them to arrays, merge those, then pass the result to successResponse() .要合并它们,您可以将它们转换为数组,合并它们,然后将结果传递给successResponse()

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

The result you want is impossible, though.但是,您想要的结果是不可能的。 The "post" object has two different values of "id". “post”对象有两个不同的“id”值。 You'll only be able to get one.你只能得到一个。 If you use如果你使用

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

Then the id value of the second object will replace the first one.然后第二个对象的 id 值将替换第一个。 If you want to keep the first id value, you need to use union instead of array_merge .如果要保留第一个 id 值,则需要使用 union 而不是array_merge

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

I think you can't concatenate 2 JSON objects as strings.我认为您不能将 2 个 JSON 对象连接为字符串。

The proper way would be to:正确的方法是:

  1. Get the Post and the PostImage objects获取 Post 和 PostImage 对象

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

    $post_images = PostImage::orderBy('id', 'DESC')->get(); $post_images = PostImage::orderBy('id', 'DESC')->get();

  2. Serialize the Post object https://laravel.com/docs/5.8/eloquent-serialization序列化 Post 对象https://laravel.com/docs/5.8/eloquent-serialization

  3. Use the method described below to add each field of the PostImage object (image_name1 .. image_name5) to the JSON How to add attribute in JSON in PHP?使用下面描述的方法将 PostImage 对象的每个字段 (image_name1 .. image_name5) 添加到 JSON 如何在 PHP 中添加 JSON 中的属性?

  4. Return the JSON返回 JSON

Update:更新:

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

Post::orderBy('id','DESC')->get() returns a collection of objects and it would require a different approach. Post::orderBy('id','DESC')->get() 返回一个对象集合,它需要一种不同的方法。

Try this:尝试这个:

$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);

You can definitely concatenate two JSON arrays, You have to parse the objects and concatenate them and re stringify.您绝对可以连接两个 JSON 数组,您必须解析对象并将它们连接起来并重新字符串化。

This question might be answered here also.这个问题也可以在这里回答。 https://stackoverflow.com/a/10384890/1684254 https://stackoverflow.com/a/10384890/1684254

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM