doumei7420 2016-11-10 07:58
浏览 627
已采纳

在php的json末尾添加新数据

I am trying to append new data at the end of predefined JSON. i want to append new data as an element of existing json. i mean data is appended as new element of json.

$jsonData = array();
if (!empty($json_mainQuot)) {
  $jsonData['mainQuot'] = $json_mainQuot;
}
if (!empty($json_quotation_hotel)) {
  $jsonData['quotation_hotel'] = $json_quotation_hotel;
}
$tempArray[] = json_decode($state_current_arr[0]['user_date']);
array_push($tempArray, $jsonData);
$jsonData_merged = json_encode($tempArray);

it look like this

  [{
    "id": "77",
    "agent_id": "30524",
    "raised_by": "C",
    "from_date": "2016-11-09",
    "to_date": "2016-11-10",
    "num_of_days": "1",
    "num_of_country": "1",
    "LeadConsultant": {
        "user_id": "3045",
        "lead_id": "77"
    },
    "LeadDestination": [{
        "from_date": "2016-11-09",
        "to_date": "2016-11-10",
        "country_id": "IN",
        "city_id": "67457",
        "notes": "",
        "is_hotel": "1",
        "is_sight": "1",
        "is_transfer": "1",
        "lead_id": "77"
    }]
}, {
    "mainQuot": {
        "supplier_markup": "0",
        "lead_destination_id": "168",
        "user_id": "1",
        "quot_via": 0
    },
    "quotation_hotel": {
        "lead_quot_id": "36",
        "hotel_id": "578917",
        "giata_id": "531264",
        "ratings": "3",
        "address": "Opposite ST Stand,Alibag 402201",
        "notes": "testing"
    }
}]

but i want like this

    [{
    "id": "77",
    "agent_id": "30524",
    "raised_by": "C",
    "from_date": "2016-11-09",
    "to_date": "2016-11-10",
    "LeadConsultant": {
        "user_id": "3045",
        "lead_id": "77"
    },
    "mainQuot": {
        "supplier_markup": "0",
        "lead_destination_id": "168",
        "user_id": "1",
        "quot_via": 0
    },
    "quotation_hotel": {
        "lead_quot_id": "36",
        "hotel_id": "578917",
        "giata_id": "531264",
        "ratings": "3",
        "address": "Opposite ST Stand,Alibag 402201",
        "notes": "testing"
    },
    "LeadDestination": [{
        "from_date": "2016-11-09",
        "to_date": "2016-11-10",
        "country_id": "IN",
        "city_id": "67457",
        "notes": "",
        "is_hotel": "1",
        "is_sight": "1",
        "is_transfer": "1",
        "lead_id": "77"
    }]
}]
  • 写回答

1条回答 默认 最新

  • dtdr84101 2016-11-10 08:18
    关注

    You need array_merge instead of array_push:

    <?php
    
    $state_current_arr = '{ "id": "77", "agent_id": "30524", "raised_by": "C", "from_date": "2016-11-09", "to_date": "2016-11-10", "LeadConsultant": { "user_id": "3045", "lead_id": "77" }, "LeadDestination": [ { "from_date": "2016-11-09", "to_date": "2016-11-10", "country_id": "IN", "city_id": "67457", "notes": ""}]}';
    
    $jsonData = array();
    
        $jsonData['mainQuot'] = 'test';
        $jsonData['quotation_hotel'] = 'test';
    
    $tempArray = json_decode($state_current_arr,true);
    $tempArray = array_merge($tempArray, $jsonData);
    $jsonData_merged = json_encode($tempArray);
    
    echo $jsonData_merged;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog