Introduction

Export arrays to CSV

https://github.com/dcblogdev/exportcsv

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 csv($records, $filename, $headerFields);

 

 

Support my work by donating with PayPal.

Subscribe to my newsletter

Subscribe and get my books and product announcements.

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