在 PHP 中合并两个 json

2021-12-27 00:00:00 merge json php

我有两个 json

第一个是

    [{"COLUMN_NAME":"ORDER_NO","COLUMN_TITLE":"Order Number"}
,{"COLUMN_NAME":"CUSTOMER_NO","COLUMN_TITLE":"Customer Number"}]

第二个是

[{"COLUMN_NAME":"ORDER_NO","DEFAULT_VALUE":"1521"},
{"COLUMN_NAME":"CUSTOMER_NO","DEFAULT_VALUEE":"C1435"}]

我想合并它们并有一个像

I want to merge them and have a json like

[{"COLUMN_NAME":"ORDER_NO","COLUMN_TITLE":"Order Number","DEFAULT_VALUE":"1521"}
,{"COLUMN_NAME":"CUSTOMER_NO","COLUMN_TITLE":"Customer Number","DEFAULT_VALUEE":"C1435"}]

有没有办法合并它们?如果需要更改 JSON 中的结构,对我来说也可以

is there a way to merge them? It is also OK for me if a stucture change in JSON is required

谢谢.

推荐答案

类似 json_encode(array_merge(json_decode($a, true),json_decode($b, true))) 之类的东西应该可以工作.

Something like json_encode(array_merge(json_decode($a, true),json_decode($b, true))) should work.

array_merge PHP 官方文档

array_merge in official PHP documentation

json_decode 在 PHP 官方文档中

json_decode in official PHP documentation

尝试将 true 作为第二个参数添加到 json_decode.这会将对象转换为关联数组.

try adding true as second parameter to json_decode. That'll convert objects to associative arrays.

EDIT 2:尝试 array-merge-recursive 并在下面查看我的评论.抱歉现在必须退出:(这看起来是一个完整正确的解决方案:https://stackoverflow.com/a/20286594/1466341

EDIT 2: try array-merge-recursive and see my comment below. Sorry have to log out now :( This looks like a full correct solution: https://stackoverflow.com/a/20286594/1466341

相关文章