【问题标题】:PHP: How to Add New data json data to other nested json?PHP:如何将新数据 json 数据添加到其他嵌套 json?
【发布时间】:2017-04-28 12:44:15
【问题描述】:
  • 列表项

在这里,我使用 php 从数据库中获取一些具有两个不同查询的结果,然后将其转换为 JSON 格式,现在我想将这两个 json 加入一个 json。 我想将新数据添加到我的旧 json 中,我尝试使用 array_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 中。使用 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)
      }
   ]
}

【问题讨论】:

  • 你能分享你的预期输出吗?
  • 等一下
  • 我将我的新 json2 添加到最后一行 "mycount":"1" @SahilGulati
  • 希望我的帖子能帮到你......
  • 等我试试

标签: php json


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    相关资源
    最近更新 更多