Export to CSV

David Carr

Export to CSV


Today I've released a new PHP package for exporting array data to a CSV file. Often data needs exporting from a database into CSV files. 

To install the package import it with composer:

composer require daveismyname/exportcsv

Then to use it import the namespace

use Daveismyname\ExportCsv\ExportCsv;

To export the data the following is required

//set filename
$filename = 'test.csv';

//set column names
$headerFields = array('First Name', 'Last Name', 'Company', 'Created');

//create array
$records = [];

//loop through data and add to array
foreach($contacts as $row) {
    $records[] = array(

//send params to csv 
new csv($records, $filename, $headerFields);

More details can be found at https://github.com/daveismyname/exportcsv

Laravel Modules Book by David Carr

Help support the blog so that I can continue creating new content!

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2009 - 2022 DC Blog. All code MIT license. All rights reserved.