Skip to content

Instantly share code, notes, and snippets.

@bmoren
Created April 28, 2017 15:47
Show Gist options
  • Save bmoren/dcf7776f57443f09d826f44ece63b5a3 to your computer and use it in GitHub Desktop.
Save bmoren/dcf7776f57443f09d826f44ece63b5a3 to your computer and use it in GitHub Desktop.
append a json object to an array
<?php
$elements = json_encode( $_POST['data'],JSON_NUMERIC_CHECK ) . ',';
openlog("myScriptLog", LOG_PID | LOG_PERROR, LOG_LOCAL0);
syslog(LOG_INFO, $elements);
// $test = "{
// lat: 000,
// lng: 000,
// story: 'test',
// username: 'test'
// },";
$myfile = fopen("messages.js", "r") or die("Unable to open file!");
$contents = fread($myfile,filesize("messages.js"));
fclose($myfile);
$myfile2 = fopen("messages.js", "w") or die("Unable to open file!");
$contents = rtrim($contents, "]");
// echo $contents;
$contents = $contents . $elements . "]";
// echo $contents;
fwrite($myfile2,$contents);
fclose($myfile2);
// file_put_contents('messages.js', $elements, FILE_APPEND | LOCK_EX);
// echo "appended to JSON";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment