Persisting PHP Arrays to Files with serialize, var_export, and Custom Functions
This article explains three PHP techniques for storing arrays in files—using serialize(), var_export(), and a custom array-to-string function—showing how to write, read, and reconstruct the data with complete code examples for backend developers.
In PHP, arrays can be saved to a file and later restored, providing a simple way to cache data or keep configuration without a database.
Method 1: serialize() converts the array to a storable string, writes it with file_put_contents() , and restores it using unserialize() after reading the file.
<code>$file='file_path';
$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)));
</code>Method 2: var_export() writes the array directly as valid PHP code, which can be included later; this approach is recommended for readability.
<code>$file='file_path';
$array=array('s'=>array('aaa','bbb','ccc'),'d'=>array('111','222','222'));
$text=var_export($array,true);
if(false!==fopen($file,'w+')){
file_put_contents($file,$text);
}else{
echo '创建失败';
}
</code>Method 3: Custom array-to-string function creates a standard PHP array representation, writes it to a file, and provides helper functions cache_write() , arrayeval() , and writefile() to handle formatting and file operations.
<code>$file='file_path';
$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=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.'(';
$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;
}
}
</code>All three approaches allow PHP developers to persist array data efficiently, choosing between binary serialization, human‑readable PHP code, or a custom formatted output depending on the project’s needs.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.