popphp/pop-csv

Pop CSV Component for Pop PHP Framework

3.0.0 2017-02-22 20:48 UTC

README

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