好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php中数组写入文件方法 - php数组

php中数组写入文件方法

在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' ); //跳转    } //开源代码phpfensi测试数据     else {    $fp  =  fopen ( './data/contact.cache' , 'r' ); //读     $cf  = unserialize( fread ( $fp , filesize ( './data/contact.cache' ))); //反序列化,并赋值     $this ->assign( 'cfg' , $cf ); //送到前台模板     $this ->display( 'other/contact' );   } 

查看更多关于php中数组写入文件方法 - php数组的详细内容...

  阅读:52次