title: Prevent duplicate posts within foreach
body: <p>Hello all, I'm currently working on a foreach loop that pulls images from a gallery from the backend of Wordpress. The gallery is working fine and as expected, however, I would like to prevent duplicate photos from appearing.</p>
  
  
  
  <pre><code>&lt;?php if () $query-&gt;have_posts() ):
    $thumbs = [];
  ?&gt;
  &lt;?php 
      foreach( $query-&gt;posts as $gallery ): 
        $images = get_field('gallery_images', $gallery-&gt;ID);
        if( $images ):
          foreach( $images as $image ):
            $cropped_img = aq_resize( $image['url'], 1024, 576, true, true, true );
            $img_id[] = $image['ID'];
            $thumbs[] = $cropped_img;             
  ?&gt;
  </code></pre>
  
  <p>
  Unrelated HTML to the Problem</p>
  
  <pre><code>&lt;?php endforeach; endif; endforeach; ?&gt;
  &lt;?php foreach($thumbs as $thumbnail): ?&gt;
    &lt;div class="gallery__thumb"&gt;&lt;img src="&lt;?= $thumbnail; ?&gt;"&gt;&lt;/div&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>
  Here is essentially what I'm trying to work out. I've thought to add two variables for the image ID so that they aren't duplicated in the first block of code. So that first if/foreach state looks like: </p>
  
  <pre><code>&lt;?php if( $images ):
    foreach( $images as $image ):
      $cropped_img = aq_resize( $image['url'], 1024, 576, true, true, true );
      $img_id[] = $image['ID'];
      $do_not_duplicate[] = $image['ID'];
      $thumbs[] = $cropped_img;
  ?&gt;
  </code></pre>
  
  <p>
  Followed by the followed foreach for the thumbnails</p>
  
  <pre><code>&lt;?php foreach($thumbs as $thumbnail): ?&gt;
      &lt;?php foreach($img_id as $img): ?&gt;
        &lt;?php if ($img == $do_not_duplicate) : ?&gt;
        //do nothing
        &lt;?php else : ?&gt;
        &lt;div class="gallery__thumb"&gt;&lt;img src="&lt;?= $thumbnail; ?&gt;"&gt;&lt;/div&gt;
        &lt;?php endif; ?&gt;
  &lt;?php endforeach; endforeach; ?&gt;
  </code></pre>
  
  <p>The problem I'm experiencing with this is I'm getting 22 images outputed 23 times because there are 22 images being pulled(with 1 duplicate). Essentially I'm trying to accomplish 22 images with no duplicates.</p>
  
  <p>Any insight would be greatly appreciated, thank you in advance.</p>