How does PHP save the array as a file format? Instance Code

Source: Internet
Author: User
    1. $file = "./cache/file.cache";
    2. $array = Array ("color" = = Array ("Blue", "Red", "green"),
    3. "Size" = = Array ("Small", "medium", "large"));
    4. Cache
    5. File_put_contents ($file, serialize ($array));//write Cache
    6. Read-Out Cache
    7. $handle = fopen ($file, "R");
    8. $cacheArray = Unserialize (Fread ($handle, FileSize ($file)));
Copy Code

The second method:

Paste several functions first:

  1. Write
  2. function Cache_write ($name, $var, $values) {
  3. $cachefile = S_root. /data/data_ '. $name. PHP ';
  4. $cachetext = " " if (!defined (' Check_code ')) exit (' Access Denied '); \ r \ n ".
  5. ' $ '. $var. ' = '. Arrayeval ($values).
  6. "\r\n?>";
  7. if (!swritefile ($cachefile, $cachetext)) {
  8. Exit ("File: $cachefile write error.");
  9. }
  10. }
  11. Arrays into strings
  12. function Arrayeval ($array, $level = 0) {
  13. $space = ";
  14. for ($i = 0; $i <= $level; $i + +) {
  15. $space. = "\ T";
  16. }
  17. $evaluate = "Array\n$space (\ n";
  18. $comma = $space;
  19. foreach ($array as $key = = $val) {
  20. $key = is_string ($key)? ' \ '. Addcslashes ($key, ' \ \ \ '). ' \ ': $key;
  21. $val =!is_array ($val) && (!preg_match ("/^\-?\d+$/", $val) | | strlen ($VAL) > 12)? ' \ '. Addcslashes ($val, ' \ \ \ '). ' \ ': $val;
  22. if (Is_array ($val)) {
  23. $evaluate. = "$comma $key =". Arrayeval ($val, $level + 1);
  24. } else {
  25. $evaluate. = "$comma $key = $val";
  26. }
  27. $comma = ", \n$space";
  28. }
  29. $evaluate. = "\n$space)";
  30. return $evaluate;
  31. }
  32. Write file
  33. function Swritefile ($filename, $writetext, $openmod = ' W ') {
  34. if (@ $fp = fopen ($filename, $openmod)) {
  35. Flock ($FP, 2);
  36. Fwrite ($fp, $writetext);
  37. Fclose ($FP);
  38. return true;
  39. } else {
  40. Runlog (' Error ', "File: $filename write error.");
  41. return false;
  42. }
  43. }
Copy Code

Call:

Cache_write (' file ', ' Arrayname ', $array);

Include format:

@include ('./data/data_cache.php '); Array reordering sort ($arrayName);
  • 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.