Home > Backend Development > PHP Tutorial > PHP如何将数组存入缓存TXT文件并取出后还原成数组

PHP如何将数组存入缓存TXT文件并取出后还原成数组

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:25:37
Original
1057 people have browsed it

PHP开发中经常会遇到对数组的操作,有时候需要将数组数据缓存到文件中以便下次更加方便直接调用缓存的数组文件,参考代码如下:

// 写入数组
$array_1 = array(1,'55a',2,'3d6',77);
var_dump($array_1); // 输出原始数组结构
$filename="cache.txt";
$file_hwnd=fopen($filename,"w");
fwrite($file_hwnd,serialize($array_1)); //输入序列化的数据
fclose($file_hwnd);

// 开始读取并还原数组
$filename="cache.txt";
$file_hwnd=fopen($filename,"r");
$content = fread($file_hwnd, filesize($filename)); // 读去文件全部内容
fclose($file_hwnd);
$array_2 = unserialize($content); // 将文本数据转换回数组
var_dump($array_2); // 输出现在的数据结构

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template