Extracting data
To extract data from a CSV document use League\Csv\Reader
methods.
Fetching CSV data
query(callable $callable = null)
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)
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
- a specific CSV row by providing its offset; (since version 6.1)
- a non empty array directly provided;
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:
- If the number of values in a CSV row is lesser than the number of named keys, the method will add
null
values to compensate for the missing values. - If the number of values in a CSV row is greater that the number of named keys the exceeding values will be drop from the result set.
- If no argument is provided, the first row from the CSV data will be used
- If an offset is used, its content will be skipped in the result set.
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', ...]
//
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:
- the current csv row data
- the current csv key
- the current csv iterator object
$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);
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:
- must return
true
to continue iterating over the CSV; - can take up to three parameters:
- the current csv row data;
- the current csv key;
- the current csv iterator object;
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;
});