I know you say that you need to do it the way you described, but I'll throw out this option anyway. Have you considered using SELECT INTO OUTFILE
syntax? MySQL can generate the CSV file for you (if you're not using MySQL, there's probably similar functionality you can find):
SELECT * INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;
You could do an fpassthru() after that if you wanted to dump out the file.
If you can't get around loading your entire data set into memory, then you can specifically unset() your array to be sure that it is cleared after you are done with it. As Havenard mentioned however, if you are loading this from a web page, then your web server will automatically clear the memory when the PHP script finishes executing, as the web thread will shut down the PHP environment.