在 PHP 中合并两个 json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20286208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:01:52  来源:igfitidea点击:

Merging two json in PHP

phpjsonmerge

提问by Calipso

I have two json's

我有两个json

First one is

第一个是

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

Second one is

第二个是

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

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

有没有办法合并它们?如果需要改变 JSON 的结构,我也可以

thanks.

谢谢。

回答by DarkSide

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

类似的东西json_encode(array_merge(json_decode($a, true),json_decode($b, true)))应该工作。

array_mergein official PHP documentation

PHP 官方文档中的array_merge

json_decodein official PHP documentation

PHP 官方文档中的json_decode

EDIT:try adding trueas second parameter to json_decode. That'll convert objects to associative arrays.

编辑:尝试将true第二个参数添加到 json_decode。这会将对象转换为关联数组。

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

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

回答by Ben Fortune

Managed to throw this together. There is most likely a better solution, but this is the closest I got.

设法把它放在一起。很可能有更好的解决方案,但这是我得到的最接近的解决方案。

$a = '[{"COLUMN_NAME":"ORDER_NO","COLUMN_TITLE":"Order Number"},{"COLUMN_NAME":"CUSTOMER_NO","COLUMN_TITLE":"Customer Number"}]';
$b = '[{"COLUMN_NAME":"ORDER_NO","DEFAULT_VALUE":"1521"},{"COLUMN_NAME":"CUSTOMER_NO","DEFAULT_VALUEE":"C1435"}]';
$r = [];
foreach(json_decode($a, true) as $key => $array){
 $r[$key] = array_merge(json_decode($b, true)[$key],$array);
}
echo json_encode($r);

returns,

返回,

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

回答by bobthenob

This works like a charm for me

这对我来说就像一个魅力

json_encode(array_merge(json_decode($a, true),json_decode($b, true)))

here is a full example

这是一个完整的例子

$query="SELECT * FROM `customer` where patient_id='1111118'";

$mysql_result = mysql_query($query);

$rows = array();
while($r = mysql_fetch_assoc($mysql_result)) {
    $rows[] = $r;
}
$json_personal_information=json_encode($rows);
//echo $json_personal_information;




$query="SELECT * FROM `doctor` where patient_id='1111118'";

$mysql_result = mysql_query($query);

$rows = array();
while($r = mysql_fetch_assoc($mysql_result)) {
    $rows[] = $r;
}
$json_doctor_information=json_encode($rows);
//echo $json_doctor_information;

echo $merger=json_encode(array_merge(json_decode($json_personal_information, true),json_decode($json_doctor_information, true)));

回答by Welisson Carlos Dias

This worked to me! $dados1 = json_encode(arrray('data'=>'1')); $dados2 = json_encode(arrray('data2'=>'2')); echo json_encode(array_merge(json_decode($dados1, true),json_decode($dados2, true)));

这对我有用!$dados1 = json_encode(arrray('data'=>'1')); $dados2 = json_encode(arrray('data2'=>'2')); 回声 json_encode(array_merge(json_decode($dados1, true),json_decode($dados2, true)));