append new data at the end of json in php -


i trying append new data @ end of predefined json. want append new data element of existing json. mean data appended 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

  [{     "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 want

    [{     "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"     }] }] 

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; 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -