Found this somewhere I forgot, what this do is when you have bunch of arrays but when a quotes or something else that will mess up the writing to csv, this is the best I could find so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
function array_to_scv($array, $header_row = true, $col_sep = ",", $row_sep = "\n", $qut = '"')
{
$output = '';
if (!is_array($array) or !is_array($array[0])) return false;
//Header row.
if ($header_row)
{
foreach ($array[0] as $key => $val)
{
//Escaping quotes.
$key = str_replace($qut, "$qut$qut", $key);
$output .= "$col_sep$qut$key$qut";
}
$output = substr($output, 1)."\n";
}
//Data rows.
foreach ($array as $key => $val)
{
$tmp = '';
foreach ($val as $cell_key => $cell_val)
{
//Escaping quotes.
$cell_val = str_replace($qut, "$qut$qut", $cell_val);
$tmp .= "$col_sep$qut$cell_val$qut";
}
$output .= substr($tmp, 1).$row_sep;
}
return $output;
}
|



