This article mainly introduces three methods for storing arrays as text files in php. Each method has advantages and disadvantages. if you need them, you can refer to the php Cache variables in the form of arrays, in fact, php writes the array to a text file or the suffix is. php is stored and called directly when used. How can I use php to save an array as a text file? Below are three methods to write the php array to the file to cache the array.
(1) use serialize to serialize the array and store it as a text file. then use unserialize to restore the file.
<? Php $ file = '. /cache/phone. php '; $ array = array ('color' => array ('blue', 'red', 'green'), 'size' => array ('small ', 'Medium ', 'large'); // cache if (false! = Fopen ($ file, 'W + ') {file_put_contents ($ file, serialize ($ array )); // write cache} // read cache $ handle = fopen ($ file, 'r'); $ cacheArray = unserialize (fread ($ handle, filesize ($ file )));
(2) save the array as a standard array format. although it is complicated to save, it is easy to call.
<? Php $ file = '. /cache/phone. php '; $ array = array ('color' => array ('blue', 'red', 'green'), 'size' => array ('small ', 'Medium ', 'large'); cache_write ($ file, $ array, 'rows', false); // write function cache_write ($ filename, $ values, $ var = 'rows ', $ format = false) {$ cachefile = $ filename; $ cachetext = "<? Php \ r \ n ". '$ '. $ var. '= '. arrayeval ($ values, $ format ). ";"; return writefile ($ cachefile, $ cachetext);} // Convert the array to the string 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;} // write the 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) use var_export to directly save the array as an array and store it in a text file.
<? Php $ file = '. /cache/phone. php '; $ array = array ('color' => array ('blue', 'red', 'green'), 'size' => array ('small ', 'Medium ', 'large'); // cache $ text =' <? Php $ rows = '. var_export ($ array, true).'; if (false! = Fopen ($ file, 'W + ') {file_put_contents ($ file, $ text);} else {echo 'creation failed ';}
The above are three methods for saving arrays as text formats in php, which will be helpful for your learning.