在php中, 我們一般都是把數(shù)組存儲(chǔ)在數(shù)據(jù)庫(kù)中, 但有時(shí)也會(huì)對(duì)不經(jīng)常更新的數(shù)據(jù)存儲(chǔ)到文件中, 這樣在讀取信息時(shí)會(huì)比從數(shù)據(jù)庫(kù)中更快, 實(shí)例方法如下:
$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 );