dcblogdev/exportcsv

Export array to CSV

Fund package maintenance!
dcblogdev

v2.0.0 2020-12-27 15:10 UTC

This package is auto-updated.

Last update: 2024-10-19 19:01:00 UTC


README

Latest Version on Packagist Total Downloads

Logo

Export arrays to CSV

Install Using composer include the repository by typing the following into a terminal

composer require dcblogdev/exportcsv

Usage

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[] = [
        $row->firstName,
        $row->lastName,
        $row->companyName,
        $row->created_at
    ];
}

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

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