title: foreach loop - repeat foreach or store/echo result for second loop
body: <p>This is regarding filtering the native gallery output for WordPress from answer: <a href="https://stackoverflow.com/a/22660335/183930">here</a></p>
  
  <pre><code>// Now you loop through each attachment
  foreach ($attachments as $id =&gt; $attachment) {
  // Fetch all data related to attachment 
  $img = wp_prepare_attachment_for_js($id);
  
  // If you want a different size change 'large' to eg. 'medium'
  $url = $img['sizes']['large']['url'];
  $height = $img['sizes']['large']['height'];
  $width = $img['sizes']['large']['width'];
  $alt = $img['alt'];
  
  // Store the caption
  $caption = $img['caption'];
  
  $output .= "&lt;li&gt;\n";
  $output .= "&lt;img src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"{$alt}\" /&gt;\n";
  
  // Output the caption if it exists
  if ($caption) { 
      $output .= "&lt;div class=\"orbit-caption\"&gt;{$caption}&lt;/div&gt;\n";
  }
  
  $output .= "&lt;/li&gt;\n";
  }
  
  $output .= "&lt;/ul&gt;\n";
  $output .= "&lt;/div&gt;\n";
  
  return $output;
  
  }
  </code></pre>
  
  <p>Is there a more efficient way of repeating the foreach loop output if the results are required again, rather than just repeating the foreach loop?</p>
  
  <p>(Could use a different variable in foreach loop, but then not sure if it seems logical to disrupt concatenated output.)</p>
  
  <p>Would appreciate logic or approach for insight.</p>