JsonDatabase.class.php 477 B

12345678910111213141516
  1. <?php
  2. class JsonDatabase {
  3. public function read($filename) {
  4. $fileContents = file_get_contents(DATA_PATH . $filename);
  5. return json_decode($fileContents, true);
  6. }
  7. public function write($filename, $dataToMerge) {
  8. $originalData = $this->read($filename);
  9. $mergedData = array_merge($originalData, $dataToMerge);
  10. $fileHandle = fopen(DATA_PATH . $filename, "w");
  11. $fileText = json_encode($mergedData, true);
  12. fwrite($fileHandle, $fileText);
  13. fclose($fileHandle);
  14. }
  15. }