Answers.Body: <p>For grouping arrays, use Craft's <a href="https://craftcms.com/docs/3.x/dev/filters.html#group" rel="nofollow noreferrer">group</a> filter or <a href="https://craftcms.com/docs/3.x/dev/filters.html#index" rel="nofollow noreferrer">index</a> filter, then for joining the array items with commas use Twig's <a href="https://twig.symfony.com/doc/2.x/filters/join.html" rel="nofollow noreferrer">join</a> filter.</p>
  <p>Something like this might be what you're looking for (I've also added <a href="https://craftcms.com/docs/3.x/dev/eager-loading-elements.html" rel="nofollow noreferrer">eager loading</a> for improved performance, and I used the <a href="https://twig.symfony.com/doc/2.x/filters/map.html" rel="nofollow noreferrer">map</a> filter as a cleaner way of organising the array, though you could use a nested loop if you find it easier to read):</p>
  <pre><code>{% set featuredPeopleGrouped = book.contributors
      .featured(1)
      .with(['contribution','contributor'])
      .all()|group('contribution[0]')
  %}
  
  &lt;dl&gt;
      {% for contributionType, rowData in featuredPeopleGrouped %}
          {% set contributors = rowData|map(row =&gt; row.contributor[0].title) %}
          &lt;dt&gt;{{ contributionType }}: &lt;/dt&gt;
          &lt;dd&gt;{{ contributors|join(', ') }}&lt;/dd&gt;
      {% endfor %}
  &lt;/dl&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-09-20T21:36:12.417Z
Answers.Text: Epic -- Thank you so much for the hint about the Map filter and tip about Eager Loading. Much appreciated.
Answers.UserDisplayName: Document Services
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-09-21T06:52:19.987Z
Answers.Text: @DocumentServices My pleasure, if you could mark the answer as accepted that would be appreciated :-)
Answers.UserDisplayName: James Smith
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-09-18T08:30:49.747Z
Answers.LastActivityDate: 2020-09-21T06:53:07.730Z
Answers.LastEditDate: 2020-09-21T06:53:07.730Z
Answers.UserDisplayName: James Smith
Body: <p>I'm trying to loop through a Super Table array and group results based on a category ('contribution'). I've gotten pretty far but I'm struggling creating an array in Twig that gives me what I need (I find associative arrays very cumbersome in Twig after using PHP for so long...).</p>
  <p>Setting the contribution works. I just can't seem to write the merge rule correctly so each contributor merges into their respective role. The next step would to then join together the list of names for further output...</p>
  <p>Here is where I am currently stuck:</p>
  <pre><code>  {% set featuredContributors = [] %}
  
    {% for person in book.contributors.all() %}
      {% if person.featured == 1 %}
        {% set contribution = person.contribution.one().title %}
        {% set contributor = person.contributor.one().title %}
        {% set featuredContributors =
          featuredContributors |
            merge({
            (contribution): [contributor]
            })
        %}
      {% endif %}
    {% endfor %}
  </code></pre>
  <p>The resulting array should look like (with each array joined as a comma separated list):</p>
  <pre><code>{% set featuredContributors = {
    Editor: 'Person One, Person Two',
    Writer: 'Person Three, Person Four',    
    Other Role: 'Person Five',    
  } %}
  </code></pre>
  <p>Is there an easier way to do this? PHP via a Twig Extension? :D</p>
  
Tags: <templating><array><plugin-supertable>
Title: Grouping Super Table Results via Twig Multidimensional Array
ThreadText: # Grouping Super Table Results via Twig Multidimensional Array
  
  (Asked by: username_0 on 2020-09-17)
  
  I'm trying to loop through a Super Table array and group results based on a category ('contribution'). I've gotten pretty far but I'm struggling creating an array in Twig that gives me what I need (I find associative arrays very cumbersome in Twig after using PHP for so long...).
  
  Setting the contribution works. I just can't seem to write the merge rule correctly so each contributor merges into their respective role. The next step would to then join together the list of names for further output...
  
  Here is where I am currently stuck:
  
  ```
    {% set featuredContributors = [] %}
  
    {% for person in book.contributors.all() %}
      {% if person.featured == 1 %}
        {% set contribution = person.contribution.one().title %}
        {% set contributor = person.contributor.one().title %}
        {% set featuredContributors =
          featuredContributors |
            merge({
            (contribution): [contributor]
            })
        %}
      {% endif %}
    {% endfor %}
  
  ```
  
  The resulting array should look like (with each array joined as a comma separated list):
  
  ```
  {% set featuredContributors = {
    Editor: 'Person One, Person Two',
    Writer: 'Person Three, Person Four',    
    Other Role: 'Person Five',    
  } %}
  
  ```
  
  Is there an easier way to do this? PHP via a Twig Extension? :D
  
  **Tags:** templating, array, plugin-supertable
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-09-18. Score: 1 | Accepted answer
  
  For grouping arrays, use Craft's [group](https://craftcms.com/docs/3.x/dev/filters.html#group) filter or [index](https://craftcms.com/docs/3.x/dev/filters.html#index) filter, then for joining the array items with commas use Twig's [join](https://twig.symfony.com/doc/2.x/filters/join.html) filter.
  
  Something like this might be what you're looking for (I've also added [eager loading](https://craftcms.com/docs/3.x/dev/eager-loading-elements.html) for improved performance, and I used the [map](https://twig.symfony.com/doc/2.x/filters/map.html) filter as a cleaner way of organising the array, though you could use a nested loop if you find it easier to read):
  
  ```
  {% set featuredPeopleGrouped = book.contributors
      .featured(1)
      .with(['contribution','contributor'])
      .all()|group('contribution[0]')
  %}
  
  <dl>
      {% for contributionType, rowData in featuredPeopleGrouped %}
          {% set contributors = rowData|map(row => row.contributor[0].title) %}
          <dt>{{ contributionType }}: </dt>
          <dd>{{ contributors|join(', ') }}</dd>
      {% endfor %}
  </dl>
  
  ```
  
  > **Comments:**
  > **username_0**: Epic -- Thank you so much for the hint about the Map filter and tip about Eager Loading. Much appreciated.  
  > **username_1**: @username_0 My pleasure, if you could mark the answer as accepted that would be appreciated :-)  
  
  ---
  
  