在php中, 我們一般都是把數(shù)組存儲在數(shù)據(jù)庫中, 但有時也會對不經(jīng)常更新的數(shù)據(jù)存儲到文件中, 這樣在讀取信息時會比從數(shù)據(jù)庫中更快, 實例方法如下:
$myfile = fopen("./tmp/token.php", "w"); //文件存在,就打開,不存在就在當(dāng)前目錄的tmp目錄下創(chuàng)建token.php件 $txt = "<?php\n"; $txt .= " return array(\n"; $txt .= " 'names'=>'莊子',\n" ; $txt .= " 'sex' =>'男', \n" ; $txt .= " 'age' => 18 \n" ; $txt .= " );\n"; $txt .= "?>\n"; fwrite($myfile, $txt); //將 $txt 內(nèi)容寫入到 token.php文件 fclose($myfile);生成的結(jié)果如下:
return array( 'names'=>'莊子', 'sex' =>'男', 'age' => 18 );