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

    2 answers 2

    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); 
       $array = array(); $hand = fopen("file.csv","w"); for($i=0;$i<=count($array);$i++) { fwrite($hand,$array[$i]); } fclose($hand); 
      • if it's here, it's easier not to for, but to explode (). In the sense of: take an array, split the items separated by commas and write to the file, adding EOL at the end - kemerov4anin