[英]Merge two objects, while one is inside of an array
I need to merge 2 objects using PHP (Laravel) one of them is in an array, the other is a plain object, but also contains a nested object, is there any simple way to do it? 我需要使用PHP(Laravel)合并2个对象,其中一个是在一个数组中,另一个是普通对象,还包含一个嵌套对象,有没有简单的方法呢?
Edit: The result should be a single object 编辑:结果应该是单个对象
[
{
"id": 1,
"release_date": "1998",
"license_plate": "098HBV",
"state_id": 1,
"type_id": 7,
"created_at": "2016-11-18 11:39:19",
"updated_at": "2016-11-18 11:39:19",
"deleted_at": null
}
]
and 和
{
"id": 1,
"parent_id": null,
"name": "Chrysler",
"state_id": 1,
"type_id": 1,
"created_at": "2016-11-18 11:39:10",
"updated_at": "2016-11-18 11:39:10",
"deleted_at": null,
"pivot": {
"vehicle_id": 1,
"brand_id": 1
}
}
Desired output should be: 期望的输出应该是:
{
"id": 1,
"release_date": "1998",
"license_plate": "098HBV",
"name": "Chrysler",
}
It has to be something similar, otherwise it would break a lot of JavaScript code connected to the first object. 它必须类似,否则会破坏许多连接到第一个对象的JavaScript代码。
<?php
//Merge the two as an array
$merged = array_merge($array,(array)$object);
//As an object
$merged = (object)$merged;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.