Reputation: 65
How can i do something like this? Does someone know how to get content from another Json file and overwrite another json file with new value. I tried this but no result. Can someone help me with this, i need this its an commission from school.
the two Json files are list.json and listbackup.json. I want a code that can use listbackup.json file to overwrite list.json.
$backupFile= file_get_contents("listbackup.json");
$backupFile= json_encode($backupFile, "\n");
file_put_contents('list.json', $backupFile);
Upvotes: 0
Views: 85
Reputation: 2700
If the file is already json
you dont need to use json_encode
.
Make a backup
$listFile = file_get_contents("list.json");
file_put_contents('listbackup.json', $listFile);
Restore a backup
$backupFile = file_get_contents("listbackup.json");
file_put_contents('list.json', $backupFile);
Upvotes: 2
Reputation: 402
$backupFile = file_get_contents("listbackup.json");
$backUpJsonDecoded = json_decode( $backupFile );
// do changes
$backupJsonDecoded->option->value = 'different';
$newFileContents = json_encode( $backupJsonDecoded );
file_put_contents( 'list.json, $newFileContents );
Upvotes: 1