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

在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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?