The data here can be basic data types, arrays, objects, etc.;
You can use serialize to serialize when storing, but you must first use unserialize to deserialize when retrieving.
<?php $data = array("上海","西安","北京"); //将数组存到指定的text文件中 file_put_contents("E:/data.txt",serialize($data)); //获取数据 $datas = unserialize(file_get_contents("E:/data.txt")); print_r($datas); ?>
Of course, you can also use json_encode. The array here can be accessed as key-value pairs, and you need to use json_decode to escape when accessing.
<?php $data = array("现代"=>"上海","文化"=>"西安","首都"=>"北京"); //将数组存到指定的text文件中 file_put_contents("E:/data.txt",json_encode($data)); //获取数据 $datas = json_decode(file_get_contents("E:/data.txt")); print_r($datas); ?>