Pop CSV Component
Switch branches/tags
Nothing to show
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Update license & copyright Jan 29, 2018
tests Rework unit tests Oct 4, 2016
.gitattributes First commit Oct 3, 2016
.gitignore First commit Oct 3, 2016
.travis.yml First commit Oct 3, 2016
LICENSE.TXT Update license & copyright Jan 29, 2018
README.md Update README Feb 22, 2017
composer.json Update license Jan 29, 2018
phpunit.xml Rework unit tests Oct 4, 2016

README.md

pop-csv

Build Status Coverage Status

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);