Export arrays to CSV

Include the composer autoloader, import the ExportCsv namespace.

use Dcblogdev\ExportCsv\ExportCsv;

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

//set column names
$headerFields = ['First Name', 'Last Name', 'Company', 'Created'];

//create array
$records = [];

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

//OR set an array manually
$records[] = ['name', 'last name', 'comy', 'created'];

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

© 2006 - 2023 DC Blog. All code MIT license. All rights reserved.