php is the implementation code for storing an array in a file
- 2020-05-10 17:54:19
- OfStack
Share the code for the first time; It's badly written.. Tension, tension, tension)
php's 10-point array is powerful, and some data is not stored in the database and written directly to the file, but require when used.
(it's actually useful for storing an array in a file, but the Shared code is a little different from the actual application.)
Pay attention to the variables when you're testing. Includes defined global variables
php's 10-point array is powerful, and some data is not stored in the database and written directly to the file, but require when used.
(it's actually useful for storing an array in a file, but the Shared code is a little different from the actual application.)
<?php
function cacheArr(&$data){
if(!$data)throw new Exception(' The 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
// The storage arrays
$hello=array(1=>'test',2=>array('hello123'));
cacheArr($hello);
unset($hello);
// Reads an array of
require 'hello.arr.php';
print_r($hello);
?>
Pay attention to the variables when you're testing. Includes defined global variables