title: Emulate goto functionality in PHP 5.2 - maybe better approach?
body: <p>I have written a script using my local PHP 5.3 installation making use of the <code>goto</code> statement. Uploading it to my webserver, I had to discover that my hoster still has PHP 5.2 installed and therefore doesn't support <code>goto</code>. My question is therefore how to emulate <code>goto</code>'s functionality in the following context:</p>
  
  <pre><code>foo();
  
  iterator_start:
  
  foreach ($array as $array_item) {
      switch ($array_item) {
          case A:
              foo();
              break;
          case B:
              // Substitute
              array_splice($array, offset($array_item), 1, array(A, A, B, C));
              // Restart iterator
              goto iterator_start;
              break;
      }
  }
  </code></pre>
  
  <p>The idea is that an array must be modified according to a set of substitution rules. Once a rule has been executed (which may modify any number of array items in any position, replace them, even delete them) everything must start from zero because the entire array may have changed in an unpredictable fashion (thus recursive substitution rules are allowed as well). The solution I use, with <code>goto</code> simply jumping to the start of the loop after each substitution, seems very straightforward and even quite clean to me, but as I mentioned I cannot use it on my webserver.</p>
  
  <p>Is there any substitute for <code>goto</code> here, or can the same task be accomplished in an entirely different manner (preferably without changing too much code)?</p>
  
  <p>Any ideas are appreciated.</p>
  
  <p><br></p>
  
  <p><strong>A polite request:</strong> Please spare me any lectures on the usefulness or dangers of <code>goto</code> here. I've read <a href="https://stackoverflow.com/questions/723324/php-and-the-goto-statement-to-be-added-in-php-5-3">PHP and the goto statement to be added in PHP 5.3</a> and know about spaghetti code and all those en vogue "considered harmful" letters from the 1980s. Discussing the supposed evil of <code>goto</code> has nothing to do with my question, and treating any program construct as "bad style" <em>per se</em> is simply dogma, which has no place in my programming ideology.</p>