Skip to content

Instantly share code, notes, and snippets.

@peeyushsrj
Created March 12, 2021 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peeyushsrj/c40b8ff9c9c51d5057798dd7365a34f2 to your computer and use it in GitHub Desktop.
Save peeyushsrj/c40b8ff9c9c51d5057798dd7365a34f2 to your computer and use it in GitHub Desktop.
Read and writing to json file in php
<?php
$filename = 'filename.json';
//decode to json as associative array
$data = json_decode(file_get_contents($filename),true);
//init condition in case of empty file
if (!is_array($data)){
$data = [];
}
//operation/s on $data variables
array_push($data, "Hello world");
//encode to json
file_put_contents($filename, json_encode($blocklist));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment