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