問題描述
我有兩個 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 中的結(jié)構(gòu),對我來說也可以
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)))
之類的東西應(yīng)該可以工作.
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
作為第二個參數(shù)添加到 json_decode.這會將對象轉(zhuǎn)換為關(guān)聯(lián)數(shù)組.
try adding true
as second parameter to json_decode. That'll convert objects to associative arrays.
EDIT 2:嘗試 array-merge-recursive
并在下面查看我的評論.抱歉現(xiàn)在必須退出:(這看起來是一個完整正確的解決方案: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
這篇關(guān)于在 PHP 中合并兩個 json的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!