search
Homephp教程php手册PHP把数组写入文件实例代码

PHP把数组写入文件实例代码

Jun 13, 2016 am 09:48 AM
phpcodewriteexistExampleusBundlearraydocumentmethodofwant

在php中数组写入文件的方法有很多种,像我们要做成缓存文件就会把php数组转换成文件然后保存到.php文件,调用时直接调用此文件即可。

php把数组写入文件,通过序列和反序列实现。请看代码


$file="./data/file.cache";
file_put_contents($file,serialize($array));//写入缓存

 代码如下 复制代码

$file="./data/file.cache";
 
$array = array("count" => "3000",
 
               "num"  =>"300");
 
//缓存
 
file_put_contents($file,serialize($array));//写入缓存
?>


$file="./data/file.cache";
$handle = fopen($file, "r");
$cacheArray = unserialize(fread($handle, filesize ($file)));
print_r($cacheArray);
?>

//将一个测试的数组写入一个PHP文件:

 代码如下 复制代码

$write_array = array( '1' => 'oneone', '2' => 'two', '3' => 'three', '4' => 'four', '5' => 'five' ); //字符串处理
$string_start = ""; $string = $string_start.$string_process.$string_end; //开始写入文件
echo file_put_contents('test_array.php', $string);
?>

 
这里用到了两个函数:

 代码如下 复制代码

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() 返回的是一个字符串。

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

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

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

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

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools