ExpressionEngine® 3 User Guide

Legacy Documentation

You are using the documentation for version 3.5.17. Go here for the latest version or check here for your available upgrades to the latest version.

CSV Library

Simple Example

The CSV library is built to take in rows of data as either associative arrays or as objects and either save the data as a file or return it as a string. Each row of data is added one at a time, but you can provide any combination of associaitve arrays and objects and they can have differing keys and property names:

$csv = ee('CSV');
$csv->addRow(array(
  'email' => 'team at ellislab dot com',
  'title' => 'EllisLab Team'
))->addRow(array(
  'email' => 'hello at ellislab dot com',
  'name' => 'Hello to EllisLab'
));
echo (string) $csv;

Would result in:

"email", "title", "name"
"team at ellislab dot com", "EllisLab Team", ""
"hello at ellislab dot com", "", "Hello to EllisLab"

Alternatively you could save the resulting data to a file:

$csv->save('/path/to/file.csv');

Methods

class EllisLab\ExpressionEngine\Library\Data\CSV
EllisLab\ExpressionEngine\Library\Data\CSV::addRow($rowData)

Add a row of data to the CSV instance:

$csv->addRow(array(
  'email' => 'team at ellislab dot com',
  'title' => 'EllisLab Team'
));

OR

$row = new \stdClass();
$row->email = 'team at ellislab dot com';
$row->name = 'EllisLab Team';
$result = $csv->addRow($row);
Parameters:
  • $rowData (array/object) – A single row of data passed in as an object or as an associaitve array
Returns:

$this, the CSV object itself so you can chain ->addRow() and ->save() calls.

Return type:

Object

EllisLab\ExpressionEngine\Library\Data\CSV::save($filename)

Save the csv data to a file:

$csv->save('/path/to/file.csv');
Parameters:
  • $filename (string) – The path to the file
Returns:

TRUE if the file was saved, FALSE if there was a failure

Return type:

Boolean

EllisLab\ExpressionEngine\Library\Data\CSV::__toString()

String representation of the csv data:

echo (string) $csv;
Returns:String representation of the csv data
Return type:String