There are many ways to write an array to a file in PHP, like when we want to make a cache file, we convert the PHP array into a file and then save it to a. php file, which is called directly when called.
PHP writes an array to a file, which is implemented by sequence and inverse sequence. Please look at the code
$file = "./data/file.cache";
File_put_contents ($file, serialize ($array));//write Cache
| The code is as follows |
Copy Code |
|
$file = "./data/file.cache";
$array = Array ("Count" = "3000",
"num" = "300");
Cache
File_put_contents ($file, serialize ($array));//write Cache
?>
$file = "./data/file.cache";
$handle = fopen ($file, "R");
$cacheArray = Unserialize (Fread ($handle, FileSize ($file)));
Print_r ($cacheArray);
?> |
Write an array of tests to a PHP file:
| The code is as follows |
Copy Code |
|
$write _array = Array (' 1 ' = ' = ' Oneone ', ' 2 ' = ' + ', ' 3 ' = ' three ', ' 4 ' = ' Four ', ' 5 ' = ' five '); String processing
$string _start = " "; $string = $string _start. $string _process. $string _end;//start writing to file
echo file_put_contents (' test_array.php ', $string);
?> |
Two functions are used here:
| The code is as follows |
Copy Code |
|
1,var_export (): |
var_export-is used to output or return a string representation of a variable, and the difference between it and var_dump () is that var_export () can be used to return the structure information about the variable passed to the function, and its return representation is a valid PHP code if "echo $ string_process; ", you can see the output:
| The code is as follows |
Copy Code |
Array (1 = ' Oneone ', 2 = ' both ', 3 = ' three ', 4 = ' four ', 5 = ' five ',)
|
And it's what we're going to write to the test_array.php file (minus the PHP tag);
The var_dump () function is used to print information about a variable, it is used only to "print", not return a value, its prototype is void Var_dump (...), we come to "var_dump ($string _process);", you can see the output:
| The code is as follows |
Copy Code |
String "Array (1 = ' Oneone ', 2 = '", 3 = ' three ', 4 = ' four ', 5 = ' five ',) "
|
You can see the output of string (86) "..." again, stating that Var_export () returns a string.
http://www.bkjia.com/PHPjc/633068.html www.bkjia.com true http://www.bkjia.com/PHPjc/633068.html techarticle There are many ways to write an array to a file in PHP, like when we want to make a cache file, we convert the PHP array into a file and then save it to a. php file, which is called directly when called. P ...