Answers.Body: <p>It looks like you have a typo in your merge, and the structure of the merge is incorrect.</p>
  
  <pre><code>{% set merged = specialisms|merge(specialisms) %}
  </code></pre>
  
  <p>This is saying set merged to the array specialisms merged with itself. </p>
  
  <p>What I am guessing you want to do is merge each specialism into the merged array, which looks like this:</p>
  
  <pre><code>{% set merged = merged|merge(specialism) %}
  </code></pre>
  
  <p>Note that we are merging <code>specialism</code> and not <code>specialisms</code> (no s at the end). </p>
  
  <p>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:</p>
  
  <pre><code>{% set merged = merged|merge([specialism]) %}
  </code></pre>
  
  <p>I hope this helps!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-02T23:36:25.847Z
Answers.Text: Ah, of course! So it's basically similar to a simple `var += 1`, but with an array and a function. Problem is, I know enough to do some damage but not quite enough to get the solution! Actually, you could do away with the second loop as it is an array when I set `specialisms` (within the loop it's rendering strings). However, as I want to check each for a duplicate, I'd need the square brackets you mention, as well as an extra conditional (see my updated answer)
Answers.UserDisplayName: Rob
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-02T00:12:39.307Z
Answers.LastActivityDate: 2015-12-02T00:12:39.307Z
Answers.UserDisplayName: kcolls
Body: <p>I'm using <a href="https://github.com/aelvan/Preparse-Field-Craft" rel="nofollow noreferrer">preparse field plugin</a> to <a href="https://craftcms.stackexchange.com/questions/11749/limit-search-results-for-users-by-tag-and-limit-to-5-tags">limit the amount of tags</a> a user can add to his account. I've modified the code slightly so the tags are processed as a <code>comma, separated, list</code>.</p>
  
  <p>The plan is to integrate <a href="https://brianreavis.github.io/selectize.js/" rel="nofollow noreferrer">Selectize</a> with the search form, so that users can choose from all available tags. As I can't use <code>relatedTo</code>, 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!).</p>
  
  <p>The code below sets a variable to allow access to a merged array, I know <a href="https://craftcms.stackexchange.com/questions/5217/using-twig-to-merge-an-array-and-remove-duplicates">there's a similar post</a> (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.</p>
  
  <p>Code below:</p>
  
  <pre><code>{% 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;
  </code></pre>
  
  <p>I know I'm setting <code>merged</code> in the wrong place, as all it outputs is a duplicate of the last <code>specialisms</code> array. So how would I loop through all users and output the available tags?</p>
  
  <p>I'm probably missing some conditionals to check if the user has actually set tags, so any advice in layman's terms appreciated!</p>
  
  <p><strong>Updated:</strong> Working code (Thanks @kcolls)</p>
  
  <pre><code>{% 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;
  </code></pre>
  
  <p><a href="https://i.sstatic.net/v75pv.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/v75pv.png" alt="enter image description here"></a></p>
  
  <p>For an added bonus, sort the array alphabetically:</p>
  
  <pre><code>&lt;ul&gt;
    {% for mergedSpecialism in merged|sort %}
      &lt;li&gt;{{ mergedSpecialism }}&lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
  </code></pre>
  
Tags: <field><tags><forloop><array><merge>
Title: Correct way to merge craft.user field arrays and use outside the loop?
ThreadText: # Correct way to merge craft.user field arrays and use outside the loop?
  
  (Asked by: username_0 on 2015-11-30)
  
  I'm using [preparse field plugin](https://github.com/aelvan/Preparse-Field-Craft) to [limit the amount of tags](https://craftcms.stackexchange.com/questions/11749/limit-search-results-for-users-by-tag-and-limit-to-5-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](https://brianreavis.github.io/selectize.js/) 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](https://craftcms.stackexchange.com/questions/5217/using-twig-to-merge-an-array-and-remove-duplicates) (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 %}
  
    <ul>
      {# Test the output #}
      {% for specialism in specialisms %}
        <li>{{specialism}}</li>
      {% endfor %}
    </ul>
  {% endfor %}
  
  {# Output the merged array #}
  
  <ul>
  {% for mergedSpecialism in merged %}
    <li>{{ mergedSpecialism }}</li>
  {% endfor %}
  </ul>
  
  ```
  
  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 @username_1)
  
  ```
  {% 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 %}
  
    <ul>
      {# Test the output #}
      {% for specialism in specialisms %}
        <li>{{specialism}}</li>
      {% endfor %}
    </ul>
  {% endfor %}
  
  {# Output the merged array #}
  
  <ul style="background: blue; color: white;">
  {% for mergedSpecialism in merged %}
    <li>{{ mergedSpecialism }}</li>
  {% endfor %}
  </ul>
  
  ```
  
  [![enter image description here](https://i.sstatic.net/v75pv.png)](https://i.sstatic.net/v75pv.png)
  
  For an added bonus, sort the array alphabetically:
  
  ```
  <ul>
    {% for mergedSpecialism in merged|sort %}
      <li>{{ mergedSpecialism }}</li>
    {% endfor %}
  </ul>
  
  ```
  
  **Tags:** field, tags, forloop, array, merge
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-12-02. Score: 3 | Accepted answer
  
  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!
  
  > **Comments:**
  > **username_0**: Ah, of course! So it's basically similar to a simple `var += 1`, but with an array and a function. Problem is, I know enough to do some damage but not quite enough to get the solution! Actually, you could do away with the second loop as it is an array when I set `specialisms` (within the loop it's rendering strings). However, as I want to check each for a duplicate, I'd need the square brackets you mention, as well as an extra conditional (see my updated answer)  
  
  ---
  
  