php 数组写入文件
下面这三款php 数组 写入文件的代码,有二种是把数组以php格式保存,这样可以以php来运行此文件,还有一个是写缓存文件了,代码如下:
- $str = "<?php $list = " . var_export($list) . "; ?>";
- //写入temp.inc.php $file = 'temp.inc.php'; $fo = fopen($file, 'w'); fwrite($fl, $str); fclose($fo);
- function arrayeval($array, $level = 0) { $space = " "; for($i = 0; $i <= $level; $i++) { $space .= " "; } $evaluate = "array $space( "; $comma = "$space"; if(!emptyempty($array)){ foreach($array as $key => $val) { $val = emptyempty($val)?' ':$val; $key = is_string($key) ? "'".addcslashes($key, ''\')."'" : $key; $val = is_string($val) ? "'".addcslashes($val, ''\')."'" : $val; if(is_array($val)) { $evaluate .= "$comma$key => ".arrayeval($val, $level + 1); } else { $evaluate .= "$comma$key => $val"; } $comma = ", $space"; } } $evaluate .= " $space)"; return $evaluate; }
- $file="./www.phpfensi.com/file.cache";
- $array = array("count" => "3000", "num" =>"300");
- //缓存代码如下:
- file_put_contents($file,serialize($array));//写入缓存
- $handle = fopen($file, "r");
- $cachearray = unserialize(fread($handle, filesize ($file)));
- print_r($cachearray);