Just sharing a comprehensive function that might make your life easier working with CSV files.... please note last function argument in relation to this topic
function array2csv($data, $file = '', $download = true, $mode = 'w+', $delimiter = ',', $enclosure = '"', $escape_char = "\\", $addUnicodeBom = false){ $return = false; if ($file == '') { $f = fopen('php://memory', 'r+'); } else { $f = fopen($file, $mode); } if ($addUnicodeBom) { $utf8_with_bom = chr(239) . chr(187) . chr(191); fwrite($f, $utf8_with_bom); } foreach ($data as $line => $item) { fputcsv($f, $item, $delimiter, $enclosure, $escape_char); } rewind($f); if ($download == true) { $return = stream_get_contents($f); } else { $return = true; } return $return;}