在php中数组写入文件的方法有很多种,像我们要做成缓存文件就会把php数组转换成文件然后保存到.php文件,调用时直接调用此文件即可。
php把数组写入文件,通过序列和反序列实现,请看如下代码:
- $file="./data/file.cache";
- file_put_contents($file,serialize($array));//写入缓存
- <?php
- $file="./data/file.cache";
- $array = array("count" => "3000",
- "num" =>"300");
- //缓存
- file_put_contents($file,serialize($array));//写入缓存
- ?>
- <?
- $file="./data/file.cache";
- $handle = fopen($file, "r");
- $cacheArray = unserialize(fread($handle, filesize ($file)));
- print_r($cacheArray);
- ?>
将一个测试的数组写入一个PHP文件,代码如下:
- <?php //要写入PHP文件的数组
- $write_array = array( '1' => 'oneone', '2' => 'two', '3' => 'three', '4' => 'four', '5' => 'five' ); //字符串处理
- $string_start = "<?phpn"; $string_process = var_export($write_array, TRUE); $string_end = "n?>"; $string = $string_start.$string_process.$string_end; //开始写入文件
- echo file_put_contents('test_array.php', $string);
- ?>
这里用到了两个函数:
var_export():var_export — 用来输出或返回一个变量的字符串表示,它和 var_dump() 的区别是,var_export() 可以用来返回关于传递给该函数的变量的结构信息,并且其返回的表示是合法的 PHP 代码如果 “echo $string_process;”,则可以看到输出结果:array(1 => 'oneone', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five',),而它就是我们要写入 test_array.php 文件的内容(除去 php 标签);
var_dump()函数用来打印变量的相关信息,它只用来“打印”,而不会返回值,它的原型是 void var_dump(……),我们来 “var_dump($string_process);”,则可以看到输出结果:
string(86) "array ( 1 => 'oneone', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', )"
可以看到输出的string(86) “…”,再一次说明了 var_export() 返回的是一个字符串.
波比源码 » PHP把数组写入文件实例代码
order levofloxacin 500mg pills buy levofloxacin without prescription
anastrozole 1mg price order arimidex 1mg online sildenafil 100mg canada
buy accutane for sale azithromycin over the counter stromectol coronavirus
order modafinil 200mg without prescription modafinil for sale online buy diamox 250 mg online
ramipril 5mg over the counter ramipril 5mg uk buy astelin 10 ml generic
catapres 0.1 mg for sale order generic meclizine 25 mg spiriva 9mcg us
purchase alendronate pill oral famotidine 40mg pepcid 20mg generic
generic cialis cost amantadine 100 mg drug symmetrel pills
order piracetam without prescription piracetam generic buy viagra 50mg for sale
order cialis generic tadalafil women cheap sildenafil 50mg
cheap generic tadalafil buy clomipramine 50mg for sale buy clomipramine 25mg generic
metformin oral cialis 10mg canada tadalafil 40mg price
brand clozaril buy clozaril online cheap order dexamethasone 0,0,5 mg for sale
order vardenafil 10mg without prescription medrol online methylprednisolone order online
buy cialis 20mg online tadalafil 40mg oral purchase plavix
buy allopurinol 300mg online buy rosuvastatin 10mg generic ezetimibe 10mg price
order cozaar 50mg online cozaar price topiramate 100mg pills
order baclofen generic cost toradol 10mg buy toradol
buy colchicine pill casino games online real money slot games online free
tamsulosin cost flomax 0.2mg pill purchase aldactone pills
generic cialis 20mg ampicillin 250mg cheap buy ciprofloxacin online
zocor price order zocor 10mg online cheap finasteride 5mg brand
viagra in usa cheap sildenafil 50mg purchase cialis online
furosemide drug plaquenil 200mg uk hydroxychloroquine online buy