下面這三款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.49028c.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);
新聞熱點
疑難解答