下面这三款php 数组 写入文件的代码,有二种是把数组以php格式保存,这样可以以php来运行此文件,还有一个是写缓存文件了,
代码如下 | 复制代码 |
$str = ""; //写入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 $val) { $val = empty($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.111cn.net/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); |