Created
March 12, 2021 18:46
-
-
Save peeyushsrj/c40b8ff9c9c51d5057798dd7365a34f2 to your computer and use it in GitHub Desktop.
Read and writing to json file in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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