PHP stores the array as a text file method summary, _php tutorial

Source: Internet
Author: User

PHP stores the array as a text file method summary,


PHP cache array of variables, in effect, is to write the array of PHP to a text file or a suffix called. php is stored, directly call this file when used. So how do you use PHP to save an array as a text-formatted file? Three ways to write a PHP array to a file to cache an array are shared below.
(1) Use Serialize to store array serialization as a text file, and then use Unserialize restore when calling

<?php $file = './cache/phone.php '; $array =array (' color ' = = Array (' Blue ', ' red ', ' green '), ' size ' = = Array (' small ', ' medium ', ' large ')); Cache if (False!==fopen ($file, ' w+ ')) {   

(2) The self-created array is saved as a standard array format, although it is more complex to save, but simple 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);   }//array converted into 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 file function WriteFile ($filename, $writetext, $openmod = ' W ') {if (false!== $fp =fopen ($filename, $openmod)) {Flock ($f     p,2);     Fwrite ($fp, $writetext);     Fclose ($FP);   return true;   }else{return false;  } }

(3) using Var_export to store arrays directly into a text file as an array

<?php $file = './cache/phone.php '; $array =array (' color ' = = Array (' Blue ', ' red ', ' green '), ' size ' = = Array (' small ', ' medium ', ' large ')); The Cache $text = ' <?php $rows = '. Var_export ($array, true). '; '; if (False!==fopen ($file, ' w+ ')) {   file_put_contents ($file, $text);} else{   

The above is for you to introduce the three kinds of PHP to save the array as text format method, I hope that everyone's learning is helpful.

http://www.bkjia.com/PHPjc/1065568.html www.bkjia.com true http://www.bkjia.com/PHPjc/1065568.html techarticle PHP stores the array as a text file method summary, PHP cache array as a variable, is actually the PHP to write an array to a text file or a suffix called. php stored up, so ...

  • 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.