ExpressionEngine Docs

Getting Started

The Fundamentals

Advanced Usage

Best Practices

Community

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 associative arrays and objects and they can have differing keys and property names:

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

Would result in:

"email", "title", "name"
"team at example dot com", "Example Team", ""
"hello at example dot com", "", "Hello to Example"

Alternatively you could save the resulting data to a file:

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

Methods

class ExpressionEngine\Library\Data\CSV

addRow($rowData)

Add a row of data to the CSV instance.

Parameter Type Description
$rowData Array/object A single row of data passed in as an object or as an associative array
Returns Object $this, the CSV object itself so you can chain ->addRow() and
$csv->addRow(array(
 'email' => 'team at example dot com',
 'title' => 'Example Team'
));
// OR
$row = new \stdClass();
$row->email = 'team at example dot com';
$row->name = 'Example Team';
$result = $csv->addRow($row);

save($filename)

Save the csv data to a file.

Parameter Type Description
$filename String The path to the file
Returns Boolean TRUE if the file was saved, FALSE if there was a failure
$csv->save('/path/to/file.csv');

__toString()

String representation of the csv data.

Parameter Type Description
Returns String String representation of the csv data
echo (string) $csv;

ExpressionEngine 7 Docs
©2002–2024 Packet Tide,LLC.
Edit this page

AltStyle によって変換されたページ (->オリジナル) /