Since the fputcsv() function is designed to output its result to a file, I came up with this little solution for a case where I wanted to write it to a string variable (for use in an error message). Thought it might be useful to post here in case anyone else ever needs to do something similar:
private function writeArrayToCsvStr(Array $arr)
{
if(($fh = fopen('php://temp', 'rw')) == false) {
throw new Exception("Unable to fopen php://temp");
}
// might want validation here that it's a 1-D array?
$result = fputcsv($fh, $arr);
rewind($fh);
$result = fread($fh, 9999);
fclose($fh);
return $result;
}