php將數組儲存為文字檔方法匯總,_PHP教程

來源:互聯網
上載者:User

php將數組儲存為文字檔方法匯總,


php 緩衝數組形式的變數,實際上就是將 php 將數組寫入到一個文字檔或者尾碼名為 .php 儲存起來,使用的時候直接調用這個檔案。那麼如何使用 php 將數組儲存為文字格式設定的檔案呢?下面分享三種方法實現將 php 數組寫入到檔案以緩衝數組。
(1)利用serialize 將數組序列化儲存為文字檔,調用時候再使用unserialize 還原

<?php $file='./cache/phone.php'; $array=array('color'=> array('blue','red','green'),'size'=> array('small','medium','large')); //緩衝 if(false!==fopen($file,'w+')){   file_put_contents($file,serialize($array));//寫入緩衝 } //讀出緩衝 $handle=fopen($file,'r'); $cacheArray=unserialize(fread($handle,filesize($file))); 

(2)自創的將數組儲存為標準的數組格式,雖然儲存時複雜了點但是調用時簡單

<?php $file='./cache/phone.php'; $array=array('color'=> array('blue','red','green'),'size'=> array('small','medium','large')); cache_write($file,$array,'rows',false);  //寫入 function cache_write($filename,$values,$var='rows',$format=false){   $cachefile=$filename;   $cachetext="<?php\r\n".'$'.$var.'='.arrayeval($values,$format).";";   return writefile($cachefile,$cachetext); }  //數群組轉換成字串 function arrayeval($array,$format=false,$level=0){   $space=$line='';   if(!$format){     for($i=0;$i<=$level;$i++){       $space.="\t";     }     $line="\n";   }   $evaluate='Array'.$line.$space.'('.$line;   $comma=$space;   foreach($array as $key=> $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;   } } 

(3)利用 var_export 將數組直接儲存為數組形式儲存到文字檔中

<?php $file='./cache/phone.php'; $array=array('color'=> array('blue','red','green'),'size'=> array('small','medium','large')); //緩衝 $text='<?php $rows='.var_export($array,true).';'; if(false!==fopen($file,'w+')){   file_put_contents($file,$text); }else{   echo '建立失敗'; } 

以上就是為大家介紹的三種php將數組儲存為文字格式設定的方法,希望對大家的學習有所協助。

http://www.bkjia.com/PHPjc/1065568.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1065568.htmlTechArticlephp將數組儲存為文字檔方法匯總, php 緩衝數組形式的變數,實際上就是將 php 將數組寫入到一個文字檔或者尾碼名為 .php 儲存起來,使...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.