SoFunction
Updated on 2025-03-09

php method to convert arrays into csv format file output

This article describes the method of converting arrays into csv format files to output. Share it for your reference. The specific implementation method is as follows:

<?php
$sales = array( array('east','2005-01-01','2005-02-01',12.54),
        array('west','2005-01-01','2005-02-01',546.33),
        array('south','2005-01-01','2005-02-01',93.26),
        array('north','2005-01-01','2005-02-01',945.21),
        array('All Regions','--','--',1597.34) );
$fh = fopen('','w') or die("Can't open ");
foreach ($sales as $sales_line) {
  if (fputcsv($fh, $sales_line) === false) {
    die("Can't write CSV line");
  }
}
fclose($fh) or die("Can't close ");
?>

I hope this article will be helpful to everyone's PHP programming.