在php中数组写入文件的方法有很多种,像我们要做成缓存文件就会把php数组转换成文件然后保存到.php文件,调用时直接调用此文件即可。
php把数组写入文件,通过序列和反序列实现。请看代码
$file="./data/file.cache";
file_put_contents($file,serialize($array));//写入缓存
代码如下 | 复制代码 |
$file="./data/file.cache"; |
//将一个测试的数组写入一个PHP文件:
代码如下 | 复制代码 |
$write_array = array( '1' => 'oneone', '2' => 'two', '3' => 'three', '4' => 'four', '5' => 'five' ); //字符串处理 |
这里用到了两个函数:
代码如下 | 复制代码 |
1,var_export(): |
·var_export — 用来输出或返回一个变量的字符串表示,它和 var_dump() 的区别是,var_export() 可以用来返回关于传递给该函数的变量的结构信息,并且其返回的表示是合法的 PHP 代码如果 “echo $string_process;”,则可以看到输出结果:
代码如下 | 复制代码 |
array ( 1 => 'oneone', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', ) |
而它就是我们要写入 test_array.php 文件的内容(除去 php 标签);
·var_dump() 函数用来打印变量的相关信息,它只用来“打印”,而不会返回值,它的原型是 void var_dump(……),我们来 “var_dump($string_process);”,则可以看到输出结果:
代码如下 | 复制代码 |
string(86) "array ( 1 => 'oneone', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', )" |
可以看到输出的string(86) “…”,再一次说明了 var_export() 返回的是一个字符串。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools