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