title: Why does 'iterator_to_array' give different results than foreach?
body: <p>Suppose I have the following recursive function to turn an array into a Generator:</p>
  <pre class="lang-php prettyprint-override"><code>function traverse(array $items): Generator {
      if (!empty($items)) {
          yield $items[0];
  
          array_shift($items);
          yield from traverse($items);
      }
  }
  </code></pre>
  <p>Running this function and iterating over the Generator through a <code>foreach</code> gives the expected result:</p>
  <pre class="lang-php prettyprint-override"><code>$values = traverse(['a', 'b', 'c', 'd', 'e']);
  
  foreach ($values as $value) {
      echo $value;
  }
  
  // 'abcde' is echoed
  </code></pre>
  <p>However, when I use the built-in function <code>iterator_to_array()</code>, I get the following result:</p>
  <pre class="lang-php prettyprint-override"><code>$values = iterator_to_array(traverse(['a', 'b', 'c', 'd', 'e']));
  
  foreach ($values as $value) {
      echo $value;
  }
  
  // Only 'e' is echoed
  </code></pre>
  <p>I would expect both pieces of code to behave identically, yet they don't. Why is their behavior different?</p>
  <p>I am running this on PHP 8.1.</p>