To extract data from a CSV document use League\Csv\Reader methods.
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
}
The fetch method returns an Iterator.
foreach ($reader->fetch() as $row) {
//do something here
}
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 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:
null values to compensate for the missing values.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', ...]
//
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);
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 apply a callable function on each CSV row. The callable function:
true to continue iterating over the CSV;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;
});
How can I help you explore Laravel packages today?