Home  >  Article  >  Backend Development  >  php中数组写入文件方法_PHP教程

php中数组写入文件方法_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:10:37748browse

在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');
 }

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/629652.htmlTechArticle在php中为我们提供了一个函数var_export 他可以直接将php代码入到一个文件中哦。 代码如下 复制代码 var_export($times,true);后面不加true不能写入...
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