taranto/csv-parser

CSV parser for PHP7.0 or later

v1.0.0 2017-01-28 12:41 UTC

This package is not auto-updated.

Last update: 2025-06-15 14:58:40 UTC


README

Build Status Coverage Status License: MIT

CSV Parser

An easy to use CSV parser

Features

  • Converts CSV to arrays and arrays indexed by the first row cells (headers);
  • Automatically guesses CSV fields delimiter;
  • Provides limit and offset options for parsing;
  • Performance wise parsing while using as an iterator.

Requirements

  • PHP 7.0 or later.

Installation

composer require taranto/csv-parser

Usage

Parsing a CSV file to an array:

$csvParser = new CsvParser('file.csv');
$csvAsArray = $csvParser->getCsvAsArray();

Parsing a CSV file to an array indexed by the first row cells (headers):

$csvParser = new CsvParser('file.csv');
$csvAsArray = $csvParser->getCsvAsAssociativeArray();

Performance wise usage (good for large files):

  • Simple arrays
$csvParser = new CsvParser('file.csv');
$csvAsArray = [];
foreach ($csvParser as $row) {
    $csvAsArray[] = $row;
}
  • Associative arrays
$csvParser = new CsvParser('file.csv', true);
$csvAsArray = [];
foreach ($csvParser as $row) {
    $csvAsArray[] = $row;
}

Example

Given the CSV

| name  | birthdate   | 
| John  |  1985-02-03 | 
|  Kim  |  1976-05-04 | 
|  Suzy |  1991-04-02 |
|  Tom  |  1970-01-03 |

Parsing to an associtive array with offset(1) and limit(2):

$csvParser = new CsvParser('file.csv');
$csvAsArray = $csvParser->getCsvAsAssociativeArray(1, 2);

Returns:

[
    ["name" => "Kim", "birthdate" => "1976-05-04"]
    ["name" => "Suzy", "birthdate" => "1991-04-02"]
]

Author

License

This project is licensed under the MIT License - see the LICENSE.txt file for details