Home > php教程 > PHP源码 > 把数组存到文件里

把数组存到文件里

PHP中文网
Release: 2016-05-25 17:12:50
Original
1043 people have browsed it

把数组存到文件里

<?php
function cacheArr(&$data){
 if(!$data)throw new Exception(&#39;数组不能为空&#39;);

 foreach($GLOBALS as $key=>$value){
  $str=$GLOBALS[$key];
  $GLOBALS[$key]=&#39;changed&#39;;
  if(&$data==&#39;changed&#39;){
   $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:&#39;\&#39;&#39;.$key.&#39;\&#39;&#39;).&#39;=>array(&#39;;
    echoArr($value,$arrName.(is_numeric($key)?&#39;[&#39;.$key.&#39;]&#39;:&#39;[\&#39;&#39;.$key.&#39;\&#39;]&#39;));
    if($i!=$arrCount)echo &#39;),&#39;;
    else echo &#39;)&#39;;
    continue;
   }
   if($i!=$arrCount)echo ((is_numeric($key))?$key:&#39;\&#39;&#39;.$key.&#39;\&#39;&#39;).&#39;=>&#39;.(is_numeric($value)?$value:&#39;\&#39;&#39;.$value.&#39;\&#39;&#39;).&#39;,&#39;;
   else echo ((is_numeric($key))?$key:&#39;\&#39;&#39;.$key.&#39;\&#39;&#39;).&#39;=>&#39;.(is_numeric($value)?$value:&#39;\&#39;&#39;.$value.&#39;\&#39;&#39;);
  }
 }
 
 echo &#39;$&#39;.$strName.&#39;=array(&#39;;
 echoArr($data,&#39;&#39;);
 echo &#39;);&#39;;
 echo "\n?>";
 $file=fopen($strName.&#39;.arr.php&#39;,&#39;w&#39;);
 fwrite($file,ob_get_contents());
 fclose($file);
 ob_clean();
 return true;
}
?>

<?php
//存储数组
$hello=array(1=>&#39;test&#39;,2=>array(&#39;hello123&#39;));
cacheArr($hello);
unset($hello);

//读取数组
require &#39;hello.arr.php&#39;;
print_r($hello);
?>
Copy after login

                   

 以上就是把数组存到文件里的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template