PHP writes the array to the file instance code. There are many methods for writing arrays to files in php. for example, if we want to make a cache file, we will convert the php array into a file and save it to the. php file. you can directly call this file when calling the file. There are many methods for p to write files into arrays in php. for example, if we want to make a cache file, we will convert the php array into a file and save it. php file, which can be called directly.
Php writes arrays to files through sequences and reverse sequences. Please refer to the code
$ File = "./data/file. cache ";
File_put_contents ($ file, serialize ($ array); // write cache
The code is as follows: |
|
$ File = "./data/file. cache "; $ Array = array ("count" => "3000 ", & Quot; num & quot; = & quot; 300 & quot "); // 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 a test array to a php file:
The code is as follows: |
|
$ Write_array = array ('1' => 'oneone', '2' => 'two', '3' => 'Three ', '4' => 'four', '5' => 'five'); // String Processing $ String_start =" "; $ String = $ string_start. $ string_process. $ string_end; // start file writing. Echo file_put_contents ('test _ array. php', $ string ); ?> |
Two functions are used here:
The code is as follows: |
|
1, var_export (): |
· Var _ export-is used to output or return a string representation of a variable. The difference between var _ export () and var_dump () is that var_export () it can be used to return structure information about the variables passed to the function, and the returned representation is legal PHP code. if "echo $ string_process;", you can see the output result:
The code is as follows: |
|
Array (1 => 'oneone', 2 => 'two', 3 => 'Three ', 4 => 'Four', 5 => 'five ',)
|
It is the content of the file test_array.php to be written (excluding the php tag );
· The var _ dump () function is used to print information about a variable. it is only used to "print" without returning the value. its prototype is void var_dump (......), Let's click "var_dump ($ string_process);" to see the output result:
The code is as follows: |
|
String (86) "array (1 => 'oneone', 2 => 'two', 3 => 'Three ', 4 => 'Four ', 5 => 'five ',)"
|
The output string (86) "…" is displayed. Again, var_export () returns a string.
Bytes. P...