Reputation: 43
All,
I cannot for the life of me, figure out why \SplFileObject
is repeating the key in the following code. This sample uses Nowdoc as a file stream but this effect can be noted using any file.
PHP Code:
$str = <<<'EOD'
Line 1
Line 2
Line 3
Line 4
Line 5
EOD;
$file = new \SplFileObject('data://text/plain;base64,' . base64_encode($str));
$iterator = call_user_func(function (\SplFileObject $file) {
while (!$file->eof()) {
yield [$file->key() => $file->fgets()];
}
}, $file);
foreach ($iterator as $line) {
echo key($line) . ': ' . current($line) . PHP_EOL;
}
Output:
0: Line 1
0: Line 2
1: Line 3
2: Line 4
3: Line 5
Upvotes: 1
Views: 192