reenadak

2019-01-06-read-array from file update and save.

Jan 6th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. $decoded = array();
  2. $file_name = "data/".date('y-m-d').".txt";
  3.  
  4. //Retrieve the data from our text file.
  5. $fileContents = file_get_contents($file_name);
  6.  
  7. //Convert the JSON string back into an array.
  8. $decoded = json_decode($fileContents, true);
  9.  
  10. //Example array.
  11.  
  12. $decoded[] = date("H:i:s");
  13.  
  14. //Encode the array into a JSON string.
  15. $encodedString = json_encode($decoded);
  16.  
  17. //Save the JSON string to a text file.
  18. file_put_contents($file_name, $encodedString);
  19.  
  20. //The end result.
  21. var_dump($decoded);
Advertisement
Add Comment
Please, Sign In to add comment