實際上就是 php 將數(shù)組寫入到一個文本文件或者后綴名為 .php 存儲起來,使用的時候直接調(diào)用這個文件。
取mysql數(shù)據(jù)步驟忽略
(1)利用serialize將數(shù)組序列化存儲為文本文件,調(diào)用時候再使用unserialize還原
$file='文件路徑';? $array=array('s'=>?array('aaa','bbb','ccc'),'d'=>?array('111','222','222'));? if(false!==fopen($file,'w+')){? ??file_put_contents($file,serialize($array)); }? $handle=fopen($file,'r');? $cacheArray=unserialize(fread($handle,filesize($file)));
(2)利用var_export將數(shù)組直接保存為數(shù)組形式存儲到文本文件中(推薦使用)
$file='文件路徑';? $array=array('s'=>?array('aaa','bbb','ccc'),'d'=>?array('111','222','222'));? $text=' if(false!==fopen($file,'w+')){? ??file_put_contents($file,$text);? }else{? ??echo?'創(chuàng)建失敗';? }
(3)自創(chuàng)的將數(shù)組保存為標(biāo)準(zhǔn)的數(shù)組格式,雖然保存時復(fù)雜了點但是調(diào)用時簡單
立即學(xué)習(xí)“PHP免費學(xué)習(xí)筆記(深入)”;
$file='文件路徑';? $array=array('s'=>?array('aaa','bbb','ccc'),'d'=>?array('111','222','222'));? cache_write($file,$array,'rows',false);? //寫入? function?cache_write($filename,$values,$var='rows',$format=false){? ??$cachefile=$filename;? ??$cachetext=" ??return?writefile($cachefile,$cachetext);? }??? //數(shù)組轉(zhuǎn)換成字串? function?arrayeval($array,$format=false,$level=0){? ??$space=$line='';? ??if(!$format){? ????for($i=0;$i?$val){? ????$key=is_string($key)?'''.addcslashes($key,''').''':$key;? ????$val=!is_array($val)&&(!preg_match('/^-?d+$/',$val)||strlen($val)?>?12)?'''.addcslashes($val,''').''':$val;? ????if(is_array($val)){? ??????$evaluate.=$comma.$key.'=>'.arrayeval($val,$format,$level+1);? ????}else{? ??????$evaluate.=$comma.$key.'=>'.$val;? ????}? ????$comma=','.$line.$space;? ??}? ??$evaluate.=$line.$space.')';? ??return?$evaluate;? }? //寫入文件? function?writefile($filename,$writetext,$openmod='w'){? ??if(false!==$fp=fopen($filename,$openmod)){? ????flock($fp,2);? ????fwrite($fp,$writetext);? ????fclose($fp);? ????return?true;? ??}else{? ????return?false;? ??}?}
相關(guān)推薦:《mysql視頻教程》
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END
喜歡就支持一下吧
相關(guān)推薦