Correct way to merge craft.user field arrays and use outside the loop? I'm using preparse field plugin to limit the amount of tags a user can add to his account. I've modified the code slightly so the tags are processed as a comma, separated, list.

The plan is to integrate Selectize with the search form, so that users can choose from all available tags. As I can't use relatedTo, it'll have to involve some manipulation. I understand it isn't considered best practice to do this with Twig, but I'm not versed in PHP to build a plugin (I'm learning Python so Twig will have to do!).

The code below sets a variable to allow access to a merged array, I know there's a similar post (I'd also like to avoid duplicates, but Selectize will ignore them anyway) but I'm struggling to follow that example and use it for my requirements.

Code below:

{% set users = craft.users.group('verified') %}
{% set merged = [] %}

{% for user in users %}
  {% set tags = user.tagsSpecialismSearchable %}
  {% set specialisms = tags|split(', ') %}

  {% for specialism in specialisms %}
    {# Merge the tag arrays #}
    {% set merged = specialisms|merge(specialisms) %}
  {% endfor %}

  &lt;ul&gt;
    {# Test the output #}
    {% for specialism in specialisms %}
      &lt;li&gt;{{specialism}}&lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
{% endfor %}

{# Output the merged array #}

&lt;ul&gt;
{% for mergedSpecialism in merged %}
  &lt;li&gt;{{ mergedSpecialism }}&lt;/li&gt;
{% endfor %}
&lt;/ul&gt;


I know I'm setting merged in the wrong place, as all it outputs is a duplicate of the last specialisms array. So how would I loop through all users and output the available tags?

I'm probably missing some conditionals to check if the user has actually set tags, so any advice in layman's terms appreciated!

Updated: Working code (Thanks @kcolls)

{% set users = craft.users.group('verified') %}
{% set merged = [] %}

{% for user in users %}
  {% set tags = user.tagsSpecialismSearchable %}
  {% set specialisms = tags|split(', ') %}

  {% for specialism in specialisms %}
    {# Check if specialism already merged #}
    {% if specialism not in merged %}
      {# Merge each specialism #}
      {% set merged = merged|merge([specialism]) %}
    {% endif %}
  {% endfor %}

  &lt;ul&gt;
    {# Test the output #}
    {% for specialism in specialisms %}
      &lt;li&gt;{{specialism}}&lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
{% endfor %}

{# Output the merged array #}

&lt;ul style="background: blue; color: white;"&gt;
{% for mergedSpecialism in merged %}
  &lt;li&gt;{{ mergedSpecialism }}&lt;/li&gt;
{% endfor %}
&lt;/ul&gt;




For an added bonus, sort the array alphabetically:

&lt;ul&gt;
  {% for mergedSpecialism in merged|sort %}
    &lt;li&gt;{{ mergedSpecialism }}&lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;
It looks like you have a typo in your merge, and the structure of the merge is incorrect.

{% set merged = specialisms|merge(specialisms) %}


This is saying set merged to the array specialisms merged with itself. 

What I am guessing you want to do is merge each specialism into the merged array, which looks like this:

{% set merged = merged|merge(specialism) %}


Note that we are merging specialism and not specialisms (no s at the end). 

I'm not sure what data type specialism is, but if it isn't an array, you may need to wrap it in square brackets. This would become:

{% set merged = merged|merge([specialism]) %}


I hope this helps!