合并两个对象,一个在数组内

时间:2016-11-23 15:00:18

标签: php laravel object merge

我需要使用PHP(Laravel)合并2个对象,其中一个是在一个数组中,另一个是普通对象,还包含一个嵌套对象,有没有简单的方法呢?

编辑:结果应该是单个对象

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

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

期望的输出应该是:

{
   "id": 1,
   "release_date": "1998",
   "license_plate": "098HBV",
   "name": "Chrysler",

}

它必须类似,否则会破坏许多连接到第一个对象的JavaScript代码。

1 个答案:

答案 0 :(得分:0)

<?php
   //Merge the two as an array
   $merged = array_merge($array,(array)$object);
   //As an object
   $merged = (object)$merged;