Home > php教程 > php手册 > php中数组写入文件方法

php中数组写入文件方法

WBOY
Release: 2016-06-13 10:12:18
Original
891 people have browsed it

在php中为我们提供了一个函数var_export 他可以直接将php代码入到一个文件中哦。

 代码如下 复制代码

var_export($times,true);后面不加true不能写入文件哟
$fp = fopen('aa.txt','w+');
fwrite($fp,var_export($times,true));
fclose($fp);

方法二

利用serializ与unserialize函数

 代码如下 复制代码

if(isset($_POST['sub'])){    
 $cfg = array('contact'=>$_POST['contact']); //把数据存入数组   
 file_put_contents('./data/contact.cache',serialize($cfg));
        //把数组序列化之后,写到contact.cache里,
 $this->redirect('other/contact');//跳转
 }
 else{    
 $fp = fopen('./data/contact.cache','r');//读
 $cf = unserialize(fread($fp,filesize('./data/contact.cache')));//反序列化,并赋值
 $this->assign('cfg',$cf);//送到前台模板
 $this->display('other/contact');
 }

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template