PHP:如何将新数据json数据添加到其他嵌套json? 列表项
在这里,我使用php通过两个不同的查询从数据库中获取一些结果,并将其转换为JSON格式,现在我想将这两个JSON合并为一个JSON。 我想在我的旧json中添加新数据,我尝试使用PHP:如何将新数据json数据添加到其他嵌套json? 列表项,php,json,Php,Json,在这里,我使用php通过两个不同的查询从数据库中获取一些结果,并将其转换为JSON格式,现在我想将这两个JSON合并为一个JSON。 我想在我的旧json中添加新数据,我尝试使用array\u merg()合并这两个json,但没有成功,我是新手 json1: { "office_trip":[ { "vnumber":"TN22BQ6226", "vname":"Mahindra Logan", "eid":"
array\u merg()
合并这两个json,但没有成功,我是新手
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中代码>我将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)
}
]
}
你能分享你的预期输出吗?当然等一分钟,我把我的新json2添加到最后一行,名为“mycount”:“1”
@SahilGulatiHope我的帖子会帮到你……等等,我会试试的
<?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));