Entry
What is the easiest way to write out an array to a file?
Is there any inverse of file(), that is, write out an array to a file?
Apr 9th, 2001 03:24
Titus Hochgreve, Nathan Wallace,
There is no inbuilt PHP function to write an array to a file. It is
easy to do though...
The long method:
function Array2File($ary, $filename) {
if (is_array($ary)) {
$fp = @fopen($filename, "w");
if ($fp > 0) {
while (list($key, $value) = each($ary)) {
fputs($fp, "$value\n");
}
fclose($fp);
}
}
}
Funky, short technique:
// assume $file is filled with something
$fd = @fopen("output","w") or die ("I/O error");
fwrite($fd,implode("\n",$file));
fclose($fd);