比如现在有个数组:
$a[0] = "[email protected]";
$a[1] = "[email protected]";我要将它保存成1.txt1.txt的内容为:
[email protected]
[email protected]
$a[0] = "[email protected]";
$a[1] = "[email protected]";我要将它保存成1.txt1.txt的内容为:
[email protected]
[email protected]
$filename = '1.txt';// Let's make sure the file exists and is writable first.
if (is_writable($filename)) { // In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}
foreach($a as $v){
fwrite($handle, $v."\r\n");
}
fclose($handle);} else {
echo "The file $filename is not writable";
}
?>
$a[0] = "[email protected]";
$a[1] = "[email protected]";file_put_contents('1.txt', join(PHP_EOL, $a));
//json处理//写入
$a[0] = "[email protected]";
$a[1] = "[email protected]";
$jsonStr=json_encode($a);//
file_put_contents("1.txt",$jsonStr);
//读出
$fp= fopen("1.txt","r");
$num= fgets($fp);
$arr=json_decode($num);
var_dump($arr);