php - SplFileObject + LimitIterator + offset -
i have data file 2 lines (two lines example, in real, file can contain millions of lines) , use splfileobject , limititerator offseting. combination have strange behaviour in cases:
$offset = 0; $file = new \splfileobject($filepath); $fileiterator = new \limititerator($file, $offset, 100); foreach ($fileiterator $key => $line) { echo $key; } output is: 01
but $offset set 1, output blank (foreach doesn't iterate line).
my data file contain this:
{"generatedat":1434665322,"numrecords":"1} {"id":"215255","code":"nb000110"} what i'm doing wrong?
thanks
required:
use splfileobject process number of records from:
- a given start record number
- for given number of records or until
eof.
the issue splfileobject gets confused regards last record in file. prevents working correctly in foreach loops.
this code uses splfileobject , 'skip records' , 'processes records'. alas, cannot use foreach loops.
- skip number of records start of file (
$offset). - process given number of records or unit end of file (
$recordstoproccess)
the code:
<?php $filepath = __dir__ . '/q30932555.txt'; // $filepath = __dir__ . '/q30932555_1.txt'; $offset = 1; $recordstoprocess = 100; $file = new \splfileobject($filepath); // skip records $file->seek($offset); $recordsprocessed = 0; while ( ($file->valid() || strlen($file->current()) > 0) && $recordsprocessed < $recordstoprocess ) { $recordsprocessed++; echo '<br />', 'current: ', $file->key(), ' ', $file->current(); $file->next(); }
Comments
Post a Comment