title: Simplify nested loops containing external variables
body: <p>I'm trying to simplify a piece of PHP code looking like this :</p>
  
  <pre><code>public function gather($parameters, $infos)
  {
  
      $gathered = [];
      $number = false;
  
      foreach ($infos as $info) {
  
          foreach ($parameters as  $parameter) {
  
              if (strlen($parameter['number'])) {
                  $this-&gt;mark($parameter['number'], $info, $parameter, $gathered); // $gathered is passed by reference
                  $number = true;
                  continue;
              }
  
              if (strlen($parameter['default'])) {
                  $this-&gt;mark($parameter['default'], $info, $parameter, $gathered); // $gathered is passed by reference
                  continue;
              }
  
              if ($config['smth']) {
                  doSomething(...);
              }
          }
      }
  
      if (number &gt; 0) {
          doSomething(...);
      }
  
      return $gathered;
  }
  </code></pre>
  
  <p>Here are the problems I have : </p>
  
  <ul>
  <li>These nested foreaches are really ugly and I'd love to the second one in the other function. But it uses variables that are created and used outside. I already used references with <code>$gathered</code> even if it's not really good.</li>
  <li>The conditions of all ifs are different and are using different variables, do you know any way to generalize a case like this?</li>
  </ul>
  
  <p>I am ok to rewrite big parts of the functions, so I'd be happy to hear other design ideas too.</p>
  
  <p>[EDIT] The mark functions add elements to $gathered with some conditions and transformations.</p>
  
  <p>[EDIT] Added more details.</p>