Comments (6)
@alexdesignworks This is why this method is being removed in the next major release. That being said whatever the method argument the returned type was always an Iterator.
from csv.
Hi,
Thanks for using the package, what you are looking for is documented in the extracting data documentation page using the Reader::fetchAssoc
method.
$reader = \League\Csv\Reader::createFromPath('/file.csv');
foreach ($reader->fetchAssoc(0) as $row) {
//here row is an array with names from the first line
}
from csv.
@nyamsprod Yes i saw that one, only this will load one huge array which will cause memory problems on larger csv files, it would be nice if it would just return an iterator with assoc.
from csv.
Understood,
This method was created when PHP 5.5 Generator where not around. I plan to update this behavior in a upcoming release when PHP 5.4 will be EOL. In the meantime you can achieve the same result using Reader::query
method in association with Reader::fetchOne
.
$reader = \League\Csv\Reader::createFromPath('/file.csv');
$header = $reader->fetchOne();
$iterator = $reader->setOffset(1)->query(function($row) use ($header) {
return array_combine($header, $row);
});
//you are using an iterator so the memory usage shouldn't be a issue anymore
foreach ($iterator as $row) {
//here row is an array with names from the first line
}
This is a crude example of an alternative but I'm sure you get the idea. When Reader::fetchAssoc
will be updated then this code can easily be remove or updated.
This is one of the reason the Reader::query
is in the public API so that people can play around with it and come up with specific output to fullfill their own requirements 👍
from csv.
I am using the above code and it is making an array with spaces added to the end of each key - I verified the CSV does not have a space - also I can't seem to access value when using the key.
[Tracking ID ] => 724423122750
Any Idea where I am going wrong?
from csv.
@nyamsprod The fact that this method works differently based not only on the type of the first argument, but also whether it is provided or not, makes it really confusing to use. TBH, I've rarely seen such behaviour controlled by one argument.
I was expecting a behaviour like this: if not provided = defaults to 0
and retrieves the header, if array - maps keys from array. But I would never expect it to return an IteratorMap.
I'm not sure what can be done, but it is really confusing.
from csv.
Related Issues (20)
- Is Writer::insertAll() faster than Writer::insertOne() or not? HOT 2
- how to read csv without BOM? HOT 4
- getRecords from PART of columns and random sort HOT 2
- getRecords with defined header produce wrong results HOT 1
- Add object mapping HOT 12
- Enable protection against long lines HOT 6
- Preformatting data before conversion HOT 3
- TabularDataReader::getRecords()->current() can return NULL HOT 3
- php version is locked to 8.1, it causes issues with php upgrade to 8.2 or greater HOT 1
- Parsing CSV with escape character before enclosure HOT 2
- Feature request: Trim parsed csv strings HOT 2
- Since 9.12.0 TabularDataReader::getRecords is stricter - exception is thrown with string keys for header HOT 10
- Enclosure not works properly HOT 2
- Is there a simple way to format xls into csv before write/read on it
- Why does calling getContent() seem to empty the iterator returned by the Reader HOT 2
- Detect delimiter of file automatically HOT 1
- The method chunkBy is not yielding chunks correctly HOT 2
- getHeader implementation should match the documentation
- Encoding formatters (all formatters?) are not applied to the header HOT 2
- Better way to guess the delimiter HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from csv.