goodby/csv

How to skip header on csv files?

babaganoush opened this issue · 3 comments

Hello,

I can do something like

if $row[16] == "name"
{
return;
}

however was wondering if there was a way to set a skip option in config so it can skip x number of lines

thanks again for an awesome library

suin commented

Hello, @ipscott

Currently goodby/csv has no configuration to skip a specific line :(
As you know, you can skip if you write custom observer like:

use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\LexerConfig;

$config = new LexerConfig();
$lexer = new Lexer($config);

$interpreter = new Interpreter();

$lineNumber = 0;

$interpreter->addObserver(function(array $columns) use (&$lineNumber) {
    $lineNumber += 1;

    if ($lineNumber === 1) {
        return;
    }

    // treat $columns here
});

$lexer->parse('some.csv', $interpreter);

ahh haa. That makes sense. Thanks for your quick response.

emilv commented

For anyone finding this issue through search engines: Since version 1.1.0 (released November 2013) you can skip the first line with $config->setIgnoreHeaderLine(true);