php 数组写入文件(变量写入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(!empty($array)){ foreach($array as $key => $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.phprm.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); |
本文地址:http://www.phprm.com/code/34152.html
转载随意,但请附上文章地址:-)