How does PHP save an array as a file? Three ways to quickly save an array as a file store

Source: Internet
Author: User
Tags fread vars

PHP cache array of variables, in effect, is to write the array of PHP to a text file or a suffix called. php is stored, directly call this file when used. So how do you use PHP to save an array as a text-formatted file? Three ways to write a PHP array to a file to cache an array are shared below. (1) using the PHP serialization function serialize and deserialization function unserialize you can store the array as a text file, and then deserialize it out as an array. PHP Code copy content to clipboard
  1. <?php
  2. $file ='./cache/phone.php ';
  3. $array =Array ('color ' = = Array ( ' Blue ',' red ',' green '),' size ' = = Array ('  Small ',' Medium ',' large ');
  4. Cache
  5. if (false!==fopen ($file,' w+ ')) {
  6. file_put_contents ($file, serialize ($array)); Write Cache
  7. }
  8. Read-Out Cache
  9. $handle =fopen ($file,' R ');
  10. $cacheArray =unserialize (fread ($handle,filesize ($file)));
(2) The self-created array is saved as a standard array format, although it is more complex to save, but simple to callPHP Code copy content to clipboard
  1. <?php
  2. $file ='./cache/phone.php ';
  3. $array =Array ('color ' = = Array ( ' Blue ',' red ',' green '),' size ' = = Array ('  Small ',' Medium ',' large ');
  4. Cache
  5. if (false!==fopen ($file,' w+ ')) {
  6. file_put_contents ($file, serialize ($array)); Write Cache
  7. }
  8. Read-Out Cache
  9. $handle =fopen ($file,' R ');
  10. $cacheArray =unserialize (fread ($handle,filesize ($file)));
(3) using PHP's own function var_export can be stored in an array directly into a text file (recommended)PHP Code copy content to clipboard
  1. <?php
  2. $file ='./cache/phone.php ';
  3. $array =Array ('color ' = = Array ( ' Blue ',' red ',' green '),' size ' = = Array ('  Small ',' Medium ',' large ');
  4. Cache
  5. $text =' <?php $rows = '. Var_export ($array, True).'; ';
  6. if (false!==fopen ($file,' w+ ')) {
  7. file_put_contents ($file,$text);
  8. }else{
  9. echo ' creation failed ';
  10. }
Through the above three methods, more commonly used should be the third and the first, we recommend the use of a third method, because convenient, simple, fast.

PHP's
1, PHP serialization serialize format of the detailed
2. PHP methods for serializing variables four ways to serialize variables
3, PHP to write large-scale website problem set

How does PHP save an array as a file? Three ways to quickly save an array as a file store

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.