title: PHP - SplFileObject - Wrong output for second line when using seek() method
body: <p>Go to UPDATE to read what's the actual problem now. Old question was already resolved with the first answer submitted by Bert Peters.</p>
  
  <p><strong>OLD QUESTION:</strong>
  I have few files named as file.1.txt, file.2.txt, file.3.txt, ... I'm reading first file with SplFileObject and using foreach loop to iterate through its content:</p>
  
  <pre><code>$file = new SplFileObject("file.1.txt");
  foreach ($file as $row) {
    // ...
  }
  </code></pre>
  
  <p>Other files may be or may not be read, depending on the contents of the first file I'm reading. In all cases there should be only one file of others (file.2.txt or file.3.txt) which may be used in the next step. So somewhere inside foreach loop there is if statement which handles this.</p>
  
  <p>All files have the same structure, so there comes the problem. I wouldn't like to create new foreach for reading next file - as I wrote it may not be needed at all, so I would like to use existing foreach instead of writing new one. Is there any possibility to overwrite $file variable with the contents of other file and iterate over it with using only one foreach or any other loop? For example:</p>
  
  <pre><code>foreach ($file as $row) {
    // ...
    if ($contentContainsSomething) {
      $file = new SplFileObject("file.2.txt");
      // somehow reset foreach to read file.2.txt from start
    }
  }
  </code></pre>
  
  <p>I wouldn't like to use <code>goto</code> statement to solve this problem. The recursion seems to be appropriate solution, but if there's a way to change object in loop on the fly, I would prefer this solution.</p>
  
  <p><strong>UPDATE:</strong>
  As mentioned in "old question" all used files (file.1.txt, file.2.txt, ...) have the same structure, so that's why I wouldn't like to write more same loops and copy code. Instead I used code from @Danack (suggested by him on SO chat) which is already a part of solution. Here's the basic code for reading more files without any upgrade I need:</p>
  
  <pre><code>$path = "file.1.txt";
  $whileCounter = 0;
  while ($path != null) {
    $file = new SplFileObject($path);
    $file-&gt;setFlags(SplFileObject::READ_CSV);
    $file-&gt;setCsvControl("\t");
    $path = null;
    foreach ($file as $rowKey =&gt; $row) {
      // echo row  }
    $path = "file.2.txt";
    if ($whileCounter &gt; 0) {
      break; // solution to stop loop, just for now
    }
    $whileCounter++;
  }
  </code></pre>
  
  <p>So this code is working without any problem and outputs the file's lines as expected. The problem is when I would like to read next line of file with seek() method, because I would like to make decision on some information which is appended to each next line. So if I use seek($rowKey + 1) which helps me to get next line data (I use $file->current() when line is changed) and after that I call seek($rowKey) to get to previous line, then next file will output first line twice and second line will be missed. The third line and all after then are printed well. This is the problem achieved with the code below:</p>
  
  <pre><code>$path = "file.1.txt";
  $whileCounter = 0;
  while ($path != null) {
    $file = new SplFileObject($path);
    $file-&gt;setFlags(SplFileObject::READ_CSV);
    $file-&gt;setCsvControl("\t");
    $path = null;
    foreach ($file as $rowKey =&gt; $row) {
      if ($whileCounter &gt; 0) {
        var_dump($row);
        echo "&lt;br&gt;";
      }
      $file-&gt;seek($rowKey + 1);
      if ($file-&gt;valid()) {
        $file-&gt;seek($rowKey);
      } else {
        var_dump($row);
        echo "&lt;br&gt;";
        $path = "file.2.txt";
      }
    }
    $whileCounter++;
  }
  </code></pre>
  
  <p>If you apply custom .csv files (with at least five non-empty lines) instead of file.1.txt and file.2.txt, you will see that second and third output are the same (second and third output are first and "second" lines of file.2.txt). What could be wrong here?</p>