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. Php count
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.
Php writes arrays to files through sequences and reverse sequences. See the following code:
- $ File = "./data/file. cache ";
- File_put_contents ($ file, serialize ($ array); // write cache
- $ 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:
Var_export ():Var_export-is used to output or return a string representation of a variable. The difference between var_dump () and var_export () is that var_export () can be used to return structure information about the variable passed to the function, and the returned result is a valid PHP code. if "echo $ string_process;", the output result is: array (1 => 'oneone', 2 => 'two ', 3 => 'Three ', 4 => 'Four', 5 => 'five',), and it is the content of the file test_array.php to be written (excluding the php tag );
Var_dump ()A function is used to print information about a variable. it is only used to print the variable without returning the value. its prototype is void var_dump (......), Let's click "var_dump ($ string_process);" to see the output result:
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.