How to merge two JSON under an Array in PHP -


i details user through html form, , onclick form submission, run php file, save2json.php, retrieves html form, , posts json in file appointments.json.

save2json.php

$formdata = array(   $_post['doctor'] => array(     'name'=> $_post['name'],     'phone'=> $_post['phone'],            'bday'=> $_post['bday'],     'datepicker'=> $_post['datepicker'],   ) );      $filetxt = 'appointments.json';     $arr_data = array();        if(file_exists($filetxt)) {             $jsondata = file_get_contents($filetxt);    $arr_data = json_decode($jsondata, true); }            $arr_data[] = $formdata; $jsondata = json_encode($arr_data, json_pretty_print);     if(file_put_contents('appointments.json', $jsondata))  

what get: [appointments.json]

[{     "doctor #3": {         "name": "0",         "phone": "0",         "bday": "0",         "datepicker": "0"     } }, {     "doctor #3": {         "name": "1",         "phone": "1",         "bday": "1",         "datepicker": "1"     } }, {     "doctor #1": {         "name": "2",         "phone": "2",         "bday": "2",         "datepicker": "2"     } }, {     "doctor #2": {         "name": "3",         "phone": "3",         "bday": "3",         "datepicker": "3"     } }] 

what want: [appointments.json]

[{     "doctor #3": [{             "name": "0",             "phone": "0",             "bday": "0",             "datepicker": "0"         },          {             "name": "1",             "phone": "1",             "bday": "1",             "datepicker": "1"         }     ],     "doctor #1": {         "name": "2",         "phone": "2",         "bday": "2",         "datepicker": "2"     },     "doctor #2": {         "name": "3",         "phone": "3",         "bday": "3",         "datepicker": "3"     }  }] 

if under same doctor, want make both objects come under same array. , if isn't doctor 1 , doctor 2, in case, want them separate apart doctor 3 array.

thanks in advance :)

this first attempt put inside doctor array key:

<?php     // build array     $docdetails = array(         'name'=> $_post['name'],         'phone'=> $_post['phone'],         'bday'=> $_post['bday'],         'datepicker'=> $_post['datepicker'],     );      // file's content array.     $filetxt = 'appointments.json';     if(file_exists($filetxt))         $arr_data = json_decode(file_get_contents($filetxt), true);     else         $arr_data = array();     $arr_data[$_post['doctor']][] = $docdetails; ?> 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -