为了账号安全,请及时绑定邮箱和手机立即绑定

在PHP中合并两个JSON

在PHP中合并两个JSON

PHP
白猪掌柜的 2019-11-20 09:43:43
我有两个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中更改结构,对我来说也可以谢谢。
查看完整描述

3 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

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


$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);

返回,


[{"COLUMN_NAME":"ORDER_NO","DEFAULT_VALUE":"1521","COLUMN_TITLE":"Order Number"},

{"COLUMN_NAME":"CUSTOMER_NO","DEFAULT_VALUEE":"C1435","COLUMN_TITLE":"Customer Number"}]


查看完整回答
反对 回复 2019-11-20
?
慕侠2389804

TA贡献1719条经验 获得超6个赞

这对我来说就像一个魅力


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

这是一个完整的例子


$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)));


查看完整回答
反对 回复 2019-11-20
  • 3 回答
  • 0 关注
  • 1411 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信