【问题标题】:Merge two Json Object PHP (not same estructure)合并两个 Json Object PHP(结构不同)
【发布时间】:2021-12-02 08:42:56
【问题描述】:

你好,我有一个两个 json 对象的

Json 1

{
  "id": null,
  "type": "NE",
  "info": {
    "name": "Ne",
    "last_name: "NE",
    "status": "NE",
    "rfc": "NE",
    "email": "jNE"
   }
  }

json 2

  {
  "id": 2,
  "type": "PF",
  "info": {
    "status": 1,
    "rfc": "DOSLWOOEOE22",
    "email": "email@email.com"
   }
  }

我需要合并 json 对象的,当 json 2 覆盖 json 1 中的值,但 json 结果包含 json 没有的键/值

我需要同样的 json 结果:

 {
  "id": 2,
  "type": "PF",
  "info": {
    "name": "Ne",
    "last_name: "NE",
    "status": 1,
    "rfc": "DOSLWOOEOE22",
    "email": "email@email.com"
   }
  }

**注意:我尝试使用 array_merge 但不起作用,仅将 json 2 覆盖为 json 1 而无需其他键 **

【问题讨论】:

标签: php json laravel array-merge


【解决方案1】:

使用 array_replace_recursive

array_replace_recursive(json_1, json_2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多