arrays - Append javascript object to json file with php -


i have task create js object form , save data json file. have @ moment code , appends new data file if file exists. problem data appended array need append object in 1 main array.

this output (example)

[   [     {         "campaignid": "campaign1",         "id": "c1"     }   ],   [     {         "campaignid": "campaign2",         "id": "c2"     }   ] ] 

but need is

[   {      "campaignid": "campaign1",      "id": "c1"   },   {      "campaignid": "campaign2",      "id": "c2"   } ] 

i not php developer php knowledge limited , i'm googling way through task have come point google failing me.

here php code

<?php  $json = $_post['json']; $name = $_post['name']; $cat = $_post['category'];  // make requested directory // see if directory exists  $filename = "saveddata/$cat/";  if (file_exists($filename)) {    //echo "the directory {$dirname} exists"; } else {    mkdir($filename, 0777);    echo "the directory {$dirname} created."; }  $file = "saveddata/$cat/$name.json";  // append new form data in json string saved in text file  $json = json_decode($json);  $formdata = array(   $json );  $arr_data = array();        // store form data  // check if file exists if(file_exists($file)) {   // gets json-data file   $jsondata = file_get_contents($file);    // converts json string array   $arr_data = json_decode($jsondata, true); }  // appends array new form data $arr_data[] = $formdata;  // encodes array string in json format (json_pretty_print - uses whitespace in json-string, human readable) $jsondata = json_encode($arr_data, json_pretty_print);  // saves json string in $file // outputs error message if data cannot saved if(file_put_contents($file, $jsondata)) echo 'data saved';  ?> 

i keen on how fix , more importantly understand how fix desired output, awesome :)

$json put inside array here

$formdata = array(   $json ); 

and put inside array here

$arr_data[] = $formdata; 

which equivalent to

$arr_data = array(   array($json) ); 

try instead:

$arr_data[] = $json;  

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo