faqts : Computers : Programming : Languages : PHP : Function Libraries

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

19 of 19 people (100%) answered Yes
Recently 9 of 9 people (100%) answered Yes

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);