parsecsv / parsecsv-for-php

CSV data parser for PHP.
MIT License
678 stars 176 forks source link

Offset 0 anf Offset 1 returns the same result set. #216

Closed Elvinas closed 7 months ago

Elvinas commented 11 months ago

Hello :)

So to my surprise I found out that when I set offset to zero or one, I get the same exact result set, is there something I don't understand, is there any reason for this it was created like this, or maybe it's an issue.

To demonstrate this I created a csv file called test.csv, with such contents: id,firstname 1,Jerry 2,Orelia 3,Lorie

Then php file that parses CSV and prints the array:

<?php
require 'vendor/autoload.php';

$csv = new \ParseCsv\Csv();
$csv->offset = 0;
$csv->delimiter = "\t";
$csv->parseFile('test.csv');

echo "<pre>";
print_r($csv->data);
echo "</pre>";

Using $csv->offset = 0 or $csv->offset = 1, returned the same three items in the array:

Array
(
    [0] => Array
        (
            [id,firstname] => 1,Jerry
        )

    [1] => Array
        (
            [id,firstname] => 2,Orelia
        )

    [2] => Array
        (
            [id,firstname] => 3,Lorie
        )
)

If I use $csv->offset = 2; it only skips, first CSV record.

Expected behavior: I expected $csv->offset = 1 to return array items starting from second CSV record.

I don't know if this was intended behavior, maybe it is, since offset = 0, would mean to start from the first CSV record (not CSV header), and offset = 1, well technically, that could be the second CSV item, since CSV header starts at line 1, and csv records starts at LINE 2 in CSV file.

In your readme.md it states the following:

Parse data with offset

ignoring the first X (e.g. two) rows

$csv = new \ParseCsv\Csv();
$csv->offset = 2;
$csv->parseFile('data.csv');
print_r($csv->data);

So to my understanding it should skip two rows, and I should get only third record only, given my test.csv file example.

I am very sorry if I deeply misunderstood the actual behavior, since in MYSQL if I use something like: SELECT FROM table LIMIT 10 OFFSET 0
OR SELECT
FROM table LIMIT 10 OFFSET 1

OFFSET 1 skips the first record in the table, meanwhile parsecsv library doesn't. I was thinking maybe I did not set up something correctly, or maybe csv header is taken into an account somehow.

Wish you all the best :)

susgo commented 7 months ago

@Elvinas i'm not sure if i understand your question correctly and unfortunately i'm not that active in the php environment anymore.

I think the default setting is that there is a line with the column names.

The offset indicates at which data row = row number in Excel should be started - for example from row 1 (all data records) or from row 2... Of course, it depends on whether you have column names in your file. If so, the offset would be the row number in Excel minus 1.

Long story short: The offset says: Please read the data from here.

It may not be ideal.

I hope that answers your question. :-)

susgo commented 7 months ago

feel free to reopen the issue if my answer is not enaugh ;-)