Your Location is: Home > Php

PHP / Advanced Custom Fields (ACF) - Delete duplicate values

From: Mongolia View: 2748 d24 

Question

I have a website where I'm showing a list of bookmarks with multiple tags on each one, this is the output:

Bookmark Title
Link
Description text
#tag1 #tag2 #tag3 #tag4 #tag5

Bookmark Title
Link
Description text
#tag3 #tag4

Bookmark Title
Link
Description text
#tag1

(…)

I have a filter where I can select the tags to hide/show the corresponding bookmarks. The problem is now, when there a bookmarks with same tags, the tags also repeating in the filter like:

Filter: #tag1 #tag2 #tag3 #tag4 #tag5 #tag3 #tag4 #tag1

This is my filter:

<div class="d-headline--update__filter">
        <?php if( have_rows('bookmark', 'option') ):?>
            Filter:
            <span class="filter__item filter__all active" onclick="filterSelection('all')">Show all</span>
            <?php while( have_rows('bookmark', 'option') ) : the_row();?>
                            <?php $bookmarkTags = get_sub_field('bookmark_tags', 'option');
                            if( $bookmarkTags ): ?>
                                <?php foreach( $bookmarkTags as $bookmarkTag ): ?>
                                    <span onclick="filterSelection('<?php echo $bookmarkTag; ?>')" class="filter__item filter__<?php echo $bookmarkTag; ?>"><?php echo $bookmarkTag; ?></span>
                                <?php endforeach; ?>
                            <?php endif; ?>
            <?php endwhile; ?>
        <?php endif; ?>
</div>

How can I delete the duplicates in the filter?

Best answer

You need to track the tags you already displayed then conditionally display the tags if they are not already tracked. See below for one way you might do this:

<div class="d-headline--update__filter">
  <?php if( have_rows('bookmark', 'option') ):?>
    Filter:
    <span class="filter__item filter__all active" onclick="filterSelection('all')">
      Show all
    </span>

    <?php 
    
    // collection of tags for this page
    $bookmarkTagsCollection = [];
    
    while( have_rows('bookmark', 'option') ) : the_row();?>
      <?php $bookmarkTags = get_sub_field('bookmark_tags', 'option'); ?> 
      
      <?php if ( $bookmarkTags ) : ?>

        <?php foreach( $bookmarkTags as $bookmarkTag ): ?>

          <?php // only display if this tag has not been displayed yet ?>

          <?php if (!in_array($bookmarkTag, $bookmarkTagsCollection)) : ?>
            <span onclick="filterSelection('<?php echo $bookmarkTag; ?>')" class="filter__item filter__<?php echo $bookmarkTag; ?>">
              <?php echo $bookmarkTag; ?>
            </span>

            <?php 
              // add current tag to collection to it does not get displayed again if later posts also contain this tag
              $bookmarkTagsCollection[] = $bookmarkTag;
            ?>
          <?php endif; ?>
        <?php endforeach; ?>

      <?php endif; ?>
    <?php endwhile; ?>
  <?php endif; ?>
</div>