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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor