Php: storing arrays as text files _ PHP

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.