Write an array to CSV in PHP

$out = fopen('php://output', 'w');

foreach ($rows as $row) {
    fputcsv($out, $row);
}

fclose($out);