How does PHP save the array to the cache TXT file and retrieve it and restore it to an array?

Source: Internet
Author: User
PHP stores the array into a cache TXT file and retrieves it and restores it to an array. in PHP development, operations on the array are often encountered, sometimes you need to cache the array data to a file to make it easier to directly call the cached array file next time. the reference code is as follows:

// Write an array
$ Array_1 = array (1, '55a', 2, '3d6', 77 );
Var_dump ($ array_1); // outputs the original array structure.
$ Filename = "cache.txt ";
$ File_hwnd = fopen ($ filename, "w ");
Fwrite ($ file_hwnd, serialize ($ array_1); // input serialized data
Fclose ($ file_hwnd );

// Start reading and restoring the array
$ Filename = "cache.txt ";
$ File_hwnd = fopen ($ filename, "r ");
$ Content = fread ($ file_hwnd, filesize ($ filename); // read all contents of the file
Fclose ($ file_hwnd );
$ Array_2 = unserialize ($ content); // converts text data back to an array
Var_dump ($ array_2); // output the current data structure

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