Giter Club home page Giter Club logo

Comments (6)

nyamsprod avatar nyamsprod commented on August 23, 2024 1

@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.

nyamsprod avatar nyamsprod commented on August 23, 2024

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.

cdekok avatar cdekok commented on August 23, 2024

@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.

nyamsprod avatar nyamsprod commented on August 23, 2024

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.

itjunkii83 avatar itjunkii83 commented on August 23, 2024

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.

AlexSkrypnyk avatar AlexSkrypnyk commented on August 23, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.