popphp / pop-csv
Pop CSV Component for Pop PHP Framework
Installs: 2 246
Dependents: 2
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.3.0
Requires (Dev)
- phpunit/phpunit: ^9.0.0
README
OVERVIEW
pop-csv
provides a streamlined way to work with PHP data and the CSV format.
It is a component of the Pop PHP Framework.
INSTALL
Install pop-csv
using Composer.
composer require popphp/pop-csv
BASIC USAGE
Serialize Data
$phpData = [ [ 'first_name' => 'Bob', 'last_name' => 'Smith' ], [ 'first_name' => 'Jane', 'last_name' => 'Smith' ] ]; $data = new Pop\Csv\Csv($phpData); $csvString = $data->serialize();
The $csvString variable now contains:
first_name,last_name
Bob,Smith
Jane,Smith
Unserialize Data
You can either pass the data object a direct string of serialized data or a file containing a string of serialized data. It will detect which one it is and parse it accordingly.
String
$csv = new Pop\Csv\Csv($csvString); $phpData = $csv->unserialize();
Write to File
$phpData = [ ... ]; $data = new Pop\Csv\Csv($phpData); $data->serialize(); $data->writeToFile('/path/to/file.csv');
Output to HTTP
$phpData = [ ... ]; $data = new Pop\Csv\Csv($phpData); $data->serialize(); $data->outputToHttp();
Force download of file
$phpData = [ ... ]; $data = new Pop\Csv\Csv($phpData); $data->serialize('csv'); $data->outputToHttp('my-file.csv', true);