php stores a piece of data in an txt file and displays its contents
- 2021-07-10 19:04:36
- OfStack
The data here can be basic data types, arrays, objects, etc.
serialize can be used for serialization when storing, but unserialize should be used for deserialization when fetching.
<?php
$data = array(" Shanghai "," Xi'an "," Beijing ");
// Saves the array to the specified text In a file
file_put_contents("E:/data.txt",serialize($data));
// Get data
$datas = unserialize(file_get_contents("E:/data.txt"));
print_r($datas);
?>
Of course, you can also use json_encode, where arrays can be accessed as key-value pairs and escaped with json_decode.
<?php
$data = array(" Modern "=>" Shanghai "," Culture "=>" Xi'an "," Capital "=>" Beijing ");
// Saves the array to the specified text In a file
file_put_contents("E:/data.txt",json_encode($data));
// Get data
$datas = json_decode(file_get_contents("E:/data.txt"));
print_r($datas);
?>