LeagueCSV

Versions

This is the documentation for the unsupported version 7.0. Please consider upgrading your code to the latest stable version

Extracting data

To extract data from a CSV document use League\Csv\Reader methods.

By default, the mode for a Reader::createFromPath is r+ which looks for write permissions on the file and throws an Exception if the file cannot be opened with the permission set. For sake of clarity, it is strongly suggested to set r mode on the file to ensure it can be opened.

Fetching CSV data

query(callable $callable = null)

This method is deprecated since version 7.2.0 and will be remove in the next major release

The query method prepares and issues queries on the CSV data. It returns an Iterator that represents the result that you can further manipulate as you wish.

$data = $reader->query();
foreach ($data as $lineIndex => $row) {
    //do something here
}

fetch(callable $callable = null)

This method is introduced in version 7.2.0

The fetch method returns an Iterator.

foreach ($reader->fetch() as $row) {
    //do something here
}

fetchAll(callable $callable = null)

fetchAll returns a sequential array of all rows.

$data = $reader->fetchAll();
// will return something like this :
//
// [
//   ['john', 'doe', 'john.doe@example.com'],
//   ['jane', 'doe', 'jane.doe@example.com'],
//   ...
// ]
//
$nb_rows = count($data);

fetchAssoc($offset_or_keys = 0, callable $callable = null)

fetchAssoc returns a sequential array of all rows. The rows themselves are associative arrays where the keys are an one dimension array. This array must only contain unique string and/or integer values.

This array keys can be specified as the first argument as

Using a non empty array:

$data = $reader->fetchAssoc(['firstname', 'lastname', 'email']);
// will return something like this :
//
// [
//   ['firstname' => 'john', 'lastname' => 'doe', 'email' => 'john.doe@example.com'],
//   ['firstname' => 'jane', 'lastname' => 'doe', 'email' => 'jane.doe@example.com'],
//   ['firstname' => 'fred', 'lastname' => 'doe', 'email' => 'fred.doe@example.com'],
//   ...
// ]
//

Using a specific offset:

$data = $reader->fetchAssoc();
// will return something like this :
//
// [
//   ['john' => 'jane', 'doe' => 'doe', 'john.doe@example.com' => 'jane.doe@example.com'],
//   ['john' => 'fred', 'doe' => 'doe', 'john.doe@example.com' => 'fred.doe@example.com'],
//   ...
// ]
//

Of note:

fetchColumn($columnIndex = 0, callable $callable = null)

fetchColumn returns a sequential array of all values in a given column from the CSV data.

If for a given row the column does not exist, the row will be skipped.

$data = $reader->fetchColumn(2);
// will return something like this :
//
// ['john.doe@example.com', 'jane.doe@example.com', ...]
//
BC break starting with version 7.0 :
  • This method no longer adds null on an non existing column index.
  • The cell skipping is done on the callable result.

Using a callable to modify the returned resultset

The methods listed above (query, fetchAll, fetchAssoc, fetchColumn) can all take a optional callable argument to further manipulate each row before being returned. This callable function can take up to three parameters:

$data = $reader->fetchAll(function ($row) {
    return array_map('strtoupper', $row);
});
// will return something like this :
//
// [
//   ['JOHN', 'DOE', 'JOHN.DOE@EXAMPLE.COM'],
//   ['JANE', 'DOE', 'JANE.DOE@EXAMPLE.COM'],
//   ...
// ]
//
$nb_rows = count($data);

In case of the fetchAssoc method, it's the callable result which is combine to the array key.

fetchOne($offset = 0)

fetchOne return one single row from the CSV data. The required argument $offset represent the row index starting at 0. If no argument is given to the method it will return the first row from the CSV data.

$data = $reader->fetchOne(3); ///accessing the 4th row (indexing starts at 0)
// will return something like this :
//
//   ['john', 'doe', 'john.doe@example.com']
//

each(callable $callable)

each apply a callable function on each CSV row. The callable function:

The method returns the number of successful iterations.

//re-create the fetchAll method using the each method
$res = [];
$func = null;
$nbIteration = $reader->each(function ($row, $index, $iterator) use (&$res, $func)) {
    if (is_callable($func)) {
        $res[] = $func($row, $index, $iterator);
        return true;
    }
    $res[] = $row;
    return true;
});