在PHP中合并两个JSON [英] Merging two json in 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"}]
我想合并它们,并拥有
[{"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 in official PHP documentation
尝试将true
作为第二个参数添加到json_decode.这样会将对象转换为关联数组.
try adding true
as second parameter to json_decode. That'll convert objects to associative arrays.
编辑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
这篇关于在PHP中合并两个JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!