PHP:如何将新数据json数据添加到其他嵌套的json中?

时间:2017-04-28 04:23:03

标签: php json

  • 列表项

这里我使用php从两个不同的查询获取数据库的一些结果,我转换为JSON格式,现在我想将两个json加入一个json。 我想在我的旧json中添加新数据,我尝试用array_merg()合并两个jsons但没有成功,我是新手,

json1:

{  
   "office_trip":[  
      {  
         "vnumber":"TN22BQ6226",
         "vname":"Mahindra Logan",
         "eid":"4",
         "name":"kumar",
         "mobile":"7449299394",
         "tid":"1",
         "vid":"TN22BQ6226",
         "emp_id":"4",
         "pick_place":"test1",
         "start_time":"11:45 am",
         "drop_place":"test11",
         "stop_time":"01:18 pm",
         "pickupkm":"10",
         "drops":"50",
         "type_of_trip":"Cash",
         "travelkm":"40",
         "tamt":"500",
         "dates":"2017-04-27",
         "expcal":"50",
         "exp1":"",
         "exp2":"",
         "exp3":"50",
         "exp4":"50",
         "exp5":"",
         "expamt":"50",
         "expdesc":"",
         "opeing_km":"0",
         "opeing_cash":"0",
         "closing_km":"40",
         "closing_cash":"0",
         "opeing_date":"2017-04-27",
         "opeing_time":"18:03:15",
         "closing_date":"2017-04-27",
         "closing_time":"18:04:05",
         "totkm":"40",
         "totamt":"500",
         "expenses":"50",
         "handover_amt":"400",
         "balance_amt":"50",
         "handover_to":"resr",
         "plstatus":"PROFIT",
         "entry_date":"2017-04-27"
      }
   ]
}

我的数据库有另一个结果:

json2:

{"mycount":"1"}

我想将这个json2添加到json1.after中使用array_merge(json_decode($one, true),json_decode($two, true));我将json1分开并将json1分开。 如何与单个json合并。

预期输出是,

{  
   "office_trip":[  
      {  
         "vnumber":"TN22BQ6226",
         "vname":"Mahindra Logan",
         "eid":"4",
         "name":"kumar",
         "mobile":"7449299394",
         "tid":"1",
         "vid":"TN22BQ6226",
         "emp_id":"4",
         "pick_place":"test1",
         "start_time":"11:45 am",
         "drop_place":"test11",
         "stop_time":"01:18 pm",
         "pickupkm":"10",
         "drops":"50",
         "type_of_trip":"Cash",
         "travelkm":"40",
         "tamt":"500",
         "dates":"2017-04-27",
         "expcal":"50",
         "exp1":"",
         "exp2":"",
         "exp3":"50",
         "exp4":"50",
         "exp5":"",
         "expamt":"50",
         "expdesc":"",
         "opeing_km":"0",
         "opeing_cash":"0",
         "closing_km":"40",
         "closing_cash":"0",
         "opeing_date":"2017-04-27",
         "opeing_time":"18:03:15",
         "closing_date":"2017-04-27",
         "closing_time":"18:04:05",
         "totkm":"40",
         "totamt":"500",
         "expenses":"50",
         "handover_amt":"400",
         "balance_amt":"50",
         "handover_to":"resr",
         "plstatus":"PROFIT",
         "entry_date":"2017-04-27",
         "mycount":"1"(HERE WE ADD)
      }
   ]
}

1 个答案:

答案 0 :(得分:0)

Try this code snippet here

<?php

ini_set('display_errors', 1);
$string1='{  
   "office_trip":[  
      {  
         "vnumber":"TN22BQ6226",
         "vname":"Mahindra Logan",
         "eid":"4",
         "name":"kumar",
         "mobile":"7449299394",
         "tid":"1",
         "vid":"TN22BQ6226",
         "emp_id":"4",
         "pick_place":"test1",
         "start_time":"11:45 am",
         "drop_place":"test11",
         "stop_time":"01:18 pm",
         "pickupkm":"10",
         "drops":"50",
         "type_of_trip":"Cash",
         "travelkm":"40",
         "tamt":"500",
         "dates":"2017-04-27",
         "expcal":"50",
         "exp1":"",
         "exp2":"",
         "exp3":"50",
         "exp4":"50",
         "exp5":"",
         "expamt":"50",
         "expdesc":"",
         "opeing_km":"0",
         "opeing_cash":"0",
         "closing_km":"40",
         "closing_cash":"0",
         "opeing_date":"2017-04-27",
         "opeing_time":"18:03:15",
         "closing_date":"2017-04-27",
         "closing_time":"18:04:05",
         "totkm":"40",
         "totamt":"500",
         "expenses":"50",
         "handover_amt":"400",
         "balance_amt":"50",
         "handover_to":"resr",
         "plstatus":"PROFIT",
         "entry_date":"2017-04-27"
      }
   ]
}';
$json2='{"mycount":"1"}';
$array1=  json_decode($string1,true);
$array2=  json_decode($json2,true);

$array1["office_trip"][0]=array_merge($array1["office_trip"][0],$array2);
print_r(json_encode($array1,JSON_PRETTY_PRINT));