1fputs($fp, implode(",", array_map("encodeFunc", $row))."\r\n");
2
3
4
5/***
6 * @param $value array
7 * @return string array values enclosed in quotes every time.
8 */
9function encodeFunc($value) {
10 ///remove any ESCAPED double quotes within string.
11 $value = str_replace('\\"','"',$value);
12 //then force escape these same double quotes And Any UNESCAPED Ones.
13 $value = str_replace('"','\"',$value);
14 //force wrap value in quotes and return
15 return '"'.$value.'"';
16}
17
18$fp = fopen("filename.csv", 'w');
19foreach($table as $row){
20 fputs($fp, implode(",", array_map("encodeFunc", $row))."\r\n");
21}
22fclose($fp);