Implementation Code for saving php arrays to files

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. The php array is very powerful. Some data is directly written to files without being stored in the database, and require shares the code for the first time; (It is very bad to write) (actually used to store arrays in files, but the shared code is used in practical applications.

Welcome to the Linux community forum and interact with 2 million technical staff> the php array is very powerful. Some data is directly written to a file without being stored in the database, and require shares the code for the first time; (It is very bad to write) (actually used to store arrays in files, but the shared code is used in practical applications.

Welcome to the Linux community forum and interact with 2 million technicians>

Php arrays are very powerful. Some data is directly written to files without being stored in the database, and require is used directly.

I shared the code for the first time. (the code is poorly written and cannot be used)

(The function of storing arrays in files is actually used, but the shared code is a little different from the actual application)

Code 1:

  

// Convert array to file

//

Function cacheArr (& $ data ){

If (! $ Data) throw new Exception ('array cannot be blank ');

Foreach ($ GLOBALS as $ key => $ value ){

$ Str = $ GLOBALS [$ key];

$ GLOBALS [$ key] = 'changed ';

If ($ data = 'changed '){

$ StrName = $ key;

Break;

}

$ GLOBALS [$ key] = $ str;

}

Ob_clean ();

Ob_start ();

Echo"

Function echoArr ($ arr, $ arrName ){

$ ArrCount = count ($ arr); $ I = 0;

Foreach ($ arr as $ key => $ value ){

++ $ I;

If (is_array ($ value )){

Echo "\ n". (is_numeric ($ key )? $ Key: '\ ''. $ key .'\''). '=> Array (';

EchoArr ($ value, $ arrName. (is_numeric ($ key )? '['. $ Key. ']': '[\'. $ key. '\'] ');

If ($ I! = $ ArrCount) echo '),';

Else echo ')';

Continue;

}

If ($ I! = $ ArrCount) echo (is_numeric ($ key ))? $ Key: '\ ''. $ key .'\''). '=>'. (Is_numeric ($ value )? $ Value: '\ ''. $ value .'\''). ',';

Else echo (is_numeric ($ key ))? $ Key: '\ ''. $ key .'\''). '=>'. (Is_numeric ($ value )? $ Value: '\ ''. $ value .'\'');

}

}

Echo '$'. $ strName. '= array (';

EchoArr ($ data ,'');

Echo ');';

Echo "\ n?> ";

$ File = fopen ($ strName. '. arr. php', 'w ');

Fwrite ($ file, ob_get_contents ());

Fclose ($ file );

Ob_clean ();

Return true;

}

?>

Code 2:

  

// Storage array

$ Hello = array (1 => 'test', 2 => array ('hello123 '));

CacheArr ($ hello );

Unset ($ hello );

// Read the Array

//

Require 'hello. arr. php ';

Print_r ($ hello );

?>

Pay attention to variable issues during testing. Including defined global variables.

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.