PHP writes an array to a file. example code _ PHP Tutorial

Source: Internet
Author: User
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...

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.