Library to export data in various input formats to various output formats.
composer require ronrademaker/exporter
$someArray = [
['foo' => 'foo', 'bar' => 'bar'],
['foo' => 'foobar', 'bar' => 'baz'],
['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'],
['foo' => 'bar', 'baz' => 'bar'],
['foo' => 'baz', 'bar' => 'foobar'],
];
$data = new ArrayData($someArray);
$fileOption = new FileOption($outputFile);
$exporter = new CSVExporter();
$exporter->export($data, [$fileOption]);
or
$someArray = [
['foo' => 'foo', 'bar' => 'bar'],
['foo' => 'foobar', 'bar' => 'baz'],
['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'],
['foo' => 'bar', 'baz' => 'bar'],
['foo' => 'baz', 'bar' => 'foobar'],
];
$data = new ArrayData($someArray);
$fileOption = new FileOption($outputFile);
$exporter = new CSVExporter();
$exporter->setOption($fileOption);
$exporter->setInput($data);
$exporter->export();
Only baz
and foo
will be in the resulting CSV and the order of the columns will be as set in the HeadersOption.
$someArray = [
['foo' => 'foo', 'bar' => 'bar'],
['foo' => 'foobar', 'bar' => 'baz'],
['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'],
['foo' => 'bar', 'baz' => 'bar'],
['foo' => 'baz', 'bar' => 'foobar'],
];
$outputFile = 'output.csv';
$headers = new HeadersOption(['baz', 'foo']);
$data = new ArrayData($someArray);
$fileOption = new FileOption($outputFile);
$exporter = new CSVExporter();
$exporter->export($data, [$headersOption, $fileOption]);
$someArray = [
['foo' => 'foo', 'bar' => 'bar'],
['foo' => 'foobar', 'bar' => 'baz'],
['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'],
['foo' => 'bar', 'baz' => 'bar'],
['foo' => 'baz', 'bar' => 'foobar'],
];
$outputFile = 'output.csv';
$otherFile = sprintf('/data/dropbox/output-%s.csv', date('Ymd'));
$headers = new HeadersOption(['baz', 'foo']);
$data = new ArrayData($someArray);
$options = [
$headers,
new FileOption($outputFile),
new FileOption($otherFile),
];
$exporter = new CSVExporter();
$exporter->export($data, $options);
$someArray = [
['foo' => 'foo', 'bar' => 'bar', ['impossible' => 'input']],
['foo' => 'foobar', 'bar' => 'baz'],
['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'],
['foo' => 'bar', 'baz' => 'bar'],
['foo' => 'baz', 'bar' => 'foobar'],
];
$outputFile = 'output.csv';
$otherFile = sprintf('/data/dropbox/output-%s.csv', date('Ymd'));
$headers = new HeadersOption(['baz', 'foo']);
$data = new ArrayData($someArray);
$options = [
$headers,
new FileOption($outputFile),
new FileOption($otherFile),
];
try {
$exporter = new CSVExporter();
$exporter->export($data, $options);
} catch (UnsupportedDataException $e) {
echo sprintf('The given input is not valid: %s', $e->getMessage());
}
$someArray = [
['foo' => 'foo', 'bar' => 'bar'],
['foo' => 'foobar', 'bar' => 'baz'],
['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'],
['foo' => 'bar', 'baz' => 'bar'],
['foo' => 'baz', 'bar' => 'foobar'],
];
$somePluralArray = [
['foo' => 'foos', 'bar' => 'bars'],
['foo' => 'foobars', 'bar' => 'bazs'],
['foo' => 'foos', 'bar' => 'bars', 'baz' => 'bazs'],
['foo' => 'bars', 'baz' => 'bars'],
['foo' => 'bazs', 'bar' => 'foobars'],
];
$headers = new HeadersOption(['baz', 'foo']);
$exporter = new CSVExporter([$headers]);
$outputFileOption = new FileOption('output.csv');
$outputPluralFileOption = new FileOption('outputs.csv');
$inputData = new ArrayData($someArray);
$inputPluralData = new ArrayData($somePlurarData);
$exporter->setOption($outputFileOption, true);
$exporter->export($inputData);
$exporter->setOption($outputPluralFileOption, true);
$exporter->export($inputPluralData);