PHP to save the array in the file to implement code _php tips

Source: Internet
Author: User
Share the code for the first time (bad writing). Tension Turner)
PHP's array is very powerful, some data is not stored in the database directly written to the file, when used directly require.
(It's actually useful to have the array in the file, but the shared code is a little different from the actual application)
Copy Code code as follows:

<?php
Function Cachearr (& $data) {
if (! $data) throw new Exception (' array cannot be empty ');
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 "<?php\n";
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;
}
?>
<?php
Storing arrays
$hello =array (1=> ' test ', 2=>array (' hello123 '));
Cachearr ($hello);
Unset ($hello);
Reading arrays
Require ' hello.arr.php ';
Print_r ($hello);
?>

When testing, notice the variable problem. Include 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.