Foreach loop displays double categories

Hello,
I am trying to display all the tags created with a multiselect field with a unique id which is the “slug version” of the tag. Let’s say for instance that I have the tag “Exhibition design” and it needs to be displayed as a div that has and ID with a value of “exhibition-design”. I need this for a filtering system I developed on my own.

I actually did that with a foreach loop and a pluck function.

Result: sometimes the foreach loops both the slugified version and the normal one with the same ID.

For example it displays both “Exhibition design” and “exhibition-design” divs (that have the same “exhibition-design” ID).

Example:

<div id="exhibition-design">Exhibition design</div>
<div id="exhibition-design">exhibition-design</div>

This is the code I used:

<?php $alltags = $items->pluck('category', ',', true); ?>
<?php foreach($alltags as $singletag): ?>
    <?php $singletagspaces = str_replace(" ","-",$singletag); ?>
    <?php $singletaglower = strtolower($singletagspaces); ?>
    <?php $singletagchars = preg_replace('/[^A-Za-z0-9\-]/', '', $singletaglower); ?>
    <div class="single-filter category-filter" id="<?= $singletagchars ?>"><?= $singletag ?></div>
<?php endforeach ?>

What I want to achieve:
The foreach loop should only display the first tag (the one with the normal name)
<div id="exhibition-design">Exhibition design</div>

Does anyone know how to solve this?

Thank you!

Could you dump or var_dump the variable $alltags just before foreach and post the output?

I think the problem is that your category field contains values like Exhibition design and exhibition-design, otherwise you wouldn’t get the output you get from your code.

Not related to your issue, but instead of modifying the $singletag variable in multiple steps, you can use

<?= Str::slug($singletag) ?>

Hi @Adspectus,
here is the var_dump of the $alltags variable:

array(12) {
  [0]=>
  string(16) "furniture-design"
  [1]=>
  string(12) "installation"
  [2]=>
  string(12) "Architecture"
  [3]=>
  string(12) "Public space"
  [4]=>
  string(17) "Exhibition design"
  [5]=>
  string(4) "Home"
  [6]=>
  string(9) "Interiors"
  [7]=>
  string(4) "home"
  [8]=>
  string(4) "film"
  [9]=>
  string(14) "public-program"
  [10]=>
  string(21) "exhibition-design"
  [11]=>
  string(9) "interiors"
}

The things is that in the multiselect there are no predefined double choices like this.
Here are the multiselect options:

architecture: Architecture
exhibition-design: Exhibition design
furniture-design: Furniture design
installation: Installation
film: Film
publication: Publication
home: Home
interiors: Interiors
restoration: Restoration
infrastructure: Infrastructure
performance: Performance
workshop: Workshop
education: Education
public-space: Public space
public-program: Public program
event: Event
landscape: Landscape
urbanism: Urbanism

Any thoughts?

In case the $item variable is declared like this:
<?php $items = $pages->find('articles')->children(); ?>

Hi @texnixe,
as I wrote in the reply to @Adspectus there is no double category in the multiselect blueprint.

Could it be possible that categories added previously (but not present in the multiselect anymore) are saved in some way and get counted and displayed in the foreach loop despite they should not be there anymore?

Thanks for the Str::slug tip!

I see these in your dump:

Looks like you have changed the option values after they were first stored (otherwise, there shouldn’t be those near duplicates)? Check what is really stored in the content files and correct as needed. But since the values you want to store are already sluggified, I wonder why you want to sluggify them again.

Hi @texnixe,
the sluggified versions of the tags were probably entered in the multiselect predefined options in the beginning but they are not ther anymore as you can see from the multiselect options I sent you. I simply changed my mind and decided to use different tags at a certain point.

Why if I delete a multiselect option from the blueprint it is still displayed in the tags list? I thought that when someone deletes a tag it wouldn’t be displayed again (and it is not assigned to any post).

I cannot remove that suggified tag because it already not there.

What should I do to remove that from the multiselect tags list?

Thank you very much!

If your array contains home and Home or exhibition-design and Exhibition design this means that these values are stored in your content files, otherwise they wouldn’t be there.

If you select a value and later change your mind, the previously stored value will stay in your content unless you remove these values using a hook or a script, or when you next make changes to the page.

Thank you @texnixe,
I checked the “content” folder and found out that the wrong values were actually stored there.
Of course something changed on the way but I thought that when a tag is deleted from its selector it would be deleted also in the content. It seemed logical to me.

So basically when “exhibition-design” was already stored it wasn’t replaced by “Exhibition design” after I deleted “exhibition-design” option from the multiselect and added “Exhibition design” there. What happened is that in the content folder the “exhibition-design” option was still there and “Exhibition design” was missing. That is what confused me.

Thank you so much for your precious help!