php code to make csv from two dimentional array

When you fetch any data from mySQL database and make the two dimensional array from that like following

$counter = 0;
while($resRow = mysql_fetch_array($sqlRes))
{
$resArray[$counter]['ticketID'] = $resRow['ticketID'];
$resArray[$counter]['title'] = $resRow['title'];
}

Or any other two dimentional array you have in PHP and you want to generate a csv from that the solution is few easy lines of code as follows


header("Content-type:text/octect-stream");
header("Content-Disposition:attachment;filename=chargRep.csv");
for($i=0;$i<$counter;$i++)
{
print '"'.implode('","',$resArray[$i]) . "\"\n";
}
exit;

In above code the $resArray contains the two dimensional array.

Posted in |