search
Homephp教程PHP源码把数组存到文件里

把数组存到文件里

May 25, 2016 pm 05:12 PM

把数组存到文件里

<?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);
?>

                   

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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.