Php 将两个json数组合并为一个
尝试将两个JSON响应合并为一个,但问题是数据显示在两个数组中,我希望它显示在一个数组中。如何在Lumen/Laravel中实现它 尝试包含两个数组或响应Php 将两个json数组合并为一个,php,laravel,laravel-5.2,lumen,lumen-5.4,Php,Laravel,Laravel 5.2,Lumen,Lumen 5.4,尝试将两个JSON响应合并为一个,但问题是数据显示在两个数组中,我希望它显示在一个数组中。如何在Lumen/Laravel中实现它 尝试包含两个数组或响应 public function index(Request $request) { $post = Post::orderBy('id', 'DESC')->get(); $post_images = PostImage::orderBy('id', 'DESC')->get();
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"
}
得到:-
那里有一些缺失的部分,但我想我知道发生了什么 根据您得到的结果,这段代码中的
$posts
和$post\u image
似乎是有说服力的模型
return $this->successResponse($posts.$post_image );
连接它们时,它们的\uuu-toString()
方法将它们转换为字符串,这是使用toJson()
方法完成的。因此,基本上有两个JSON对象粘在一起,这不是有效的JSON,然后successResponse()
方法再次对它们进行编码
要合并它们,可以将它们转换为数组,合并它们,然后将结果传递到successResponse()
然而,你想要的结果是不可能的。“post”对象有两个不同的“id”值。你只能得到一个。如果你使用
$merged = array_merge($posts->toArray(), $post_image->toArray());
然后,第二个对象的id值将替换第一个对象。如果要保留第一个id值,则需要使用union而不是array\u merge
$merged = $a->toArray() + $b->toArray();
我认为不能将2个JSON对象连接为字符串 正确的方法是:
$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);
您可以连接两个JSON数组,您必须解析对象并连接它们并重新字符串化
这个问题也可以在这里得到回答 如果您包含源数据,可能会有所帮助,我们有预期的结果,实际的结果,但不是[源数据]-这是很好的。。。有点重要。我可以告诉你,在
json\u解码它们之后,你可能会想要使用array\u replace\u recursive($primary,$secondary)
。PS prolly不是一个词,但我喜欢它。好啊结果是:-(@ArtisticPhoenix你能帮我解决这类问题吗?不幸的是,我还没有使用Laravel,实际上我正在计划使用它完成一个项目,但由于我忙于其他事情,我们正在将其外包。但我计划以后再使用它,基本上这是我们收入的一个CRUD系统g数据,这应该很容易做到。然后我打算在其上进行构建。目前我们主要使用代码点火器2,我大约5年前构建了它并继续改进。但如果我尝试使用$post=post::orderBy('id','DESC')->get()->first()值,它工作得很好,但在删除first()时并不适用于所有值函数。它指定collectionI中不存在图像\u name1。我更新了答案。如果这对您不起作用,请详细说明您要执行的操作。您可以检查此项吗请帮助我
$merged = $a->toArray() + $b->toArray();
$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);