Write to CSV file

How to write data from an array to a CSV file?


Answer 1, authority 100%

Use the dedicated function fputcsv

<?php
$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);

Answer 2

$array = array();
$hand = fopen("file.csv","w");
for($i=0;$i<=count($array);$i++)
{
fwrite($hand,$array[$i]);
}
fclose($hand);