The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Connections Settings

Inserting Records

Selecting Records

Converting Records

Upgrading Guide

This is the documentation for the upcoming version 9.0. This is a work in progress

Constraint Builder


class Statement
    public function limit(int $limit): self
    public function offset(int $offset): self
    public function orderBy(callable $callable): self
    public function process(Reader $reader): ResultSet
    public function where(callable $callable): self

The League\Csv\Statement class is a constraint builder to help ease selecting records from a CSV document created using the League\Csv\Reader class.

When building a constraint, the methods do not need to be called in any particular order, and may be called multiple times. Because the Statement object is immutable, each time its constraint methods are called they will return a new Statement object without modifying the current Statement object.

Because the Statement object is independent of the Reader object it can be re-use on multiple Reader objects.

Filtering constraint

The filters attached using the Statement::where method are the first settings applied to the CSV before anything else. This option follow the First In First Out rule.


public Statement::where(callable $callable): self

The callable filter signature is as follows:


function(array $record [, int $offset [, Iterator $iterator]]): self

It takes up to three parameters:

Sorting constraint

The sorting options are applied after the Statement::where options. The sorting follows the First In First Out rule.

Warning: To sort the data iterator_to_array is used, which could lead to a performance penalty if you have a heavy CSV file to sort

Statement::orderBy method adds a sorting function each time it is called.


public Statement::orderBy(callable $callable): self

The callable sort function signature is as follows:


function(array $recordA, array $recordB): int

The sort function takes exactly two parameters, which will be filled by pairs of records.

Interval constraint

The interval methods enable returning a specific interval of CSV records. When called more than once, only the last filtering settings is taken into account. The interval is calculated after applying Statement::orderBy options.

The interval API is made of the following method


public Statement::offset(int $offset): self
public Statement::limit(int $limit): self

Statement::offset specifies an optional offset for the return data. By default if no offset was provided the offset equals 0.

Statement::Limit specifies an optional maximum records count for the return data. By default if no limit is provided the limit equals -1, which translate to all records.

When called multiple times, each call override the last settings for these options.

Apply the constraints to a CSV document


public Statement::process(Reader $reader): ResultSet

This method processes a Reader object and returns the found records as a ResultSet object.


use League\Csv\Reader;
use League\Csv\Statement;

function filterByEmail(array $record): bool
    return (bool) filter_var($record[2], FILTER_VALIDATE_EMAIL);

function sortByLastName(array $recordA, array $recordB): int
    return strcmp($recordB[1], $recordA[1]);

$reader = Reader::createFromPath('/path/to/file.csv');
$stmt = (new Statement())

$records = $stmt->process($reader);

This method is equivalent of Reader::select.