Answers.Body: <p>Possible, yes. But if you should do it with Twig alone is a totally different thing. There's other ways to do that in Craft,
  e.g. with relations (Entries Fields) and splitting your Products and their variants into separate sections. Or what about using a structure,
  with your variants being child entries to the product?</p>
  
  <p>But here's how you do that with your table field (totally legitimate in my opinion, one advantage being the ease of use for the Client):</p>
  
  <pre><code>{# Prepare variable before loop #}
  {% set myVariants = [] %}
  
  {% for row in myTableField %}
  
      {# Concatenate a string to the value to prevent it from being numeric #}
      {% set variantKey = 'key' ~ row.col1 %}
  
      {% if variantKey not in myVariants|keys %}
  
          {# Insert a new key and add the first value to it #}
          {% set myVariants = myVariants|merge({ (variantKey): [row.col2] }) %}
  
      {% else %}
  
          {# The key already exists, so merge the value to the existing ones #}
          {% set myVariants = myVariants|merge({ (variantKey): myVariants[variantKey]|merge([row.col2]) }) %}
  
      {% endif %}
  
  {% endfor %}
  </code></pre>
  
  <p>This is how you print it:</p>
  
  <pre><code>&lt;ul class="variantsLevel1"&gt;
      {% for variantKey, variantsLevel1 in myVariants %}
          &lt;li&gt;
  
              {# Remove the string from our key again #}
              {{ variantKey|slice(3) }}
  
              &lt;ul class="variantsLevel2"&gt;
                  {% for variantLevel2 in variantsLevel1 %}
                      &lt;li&gt;{{ variantLevel2 }}&lt;/li&gt;
                  {% endfor %}
              &lt;/ul&gt;
  
          &lt;/li&gt;
      {% endfor %}
  &lt;/ul&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-09T18:37:13.823Z
Answers.Text: Thanks so much for the response. The only problem I found with using this answer was that by using the word 'key' for the key, it truncated any variantLevel1 names that ended with k,e or y. I'm sure there is a way to force it to only look for that sequence of letters if you would like to edit the answer.
Answers.UserDisplayName: Ben
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-05T23:15:34.353Z
Answers.LastActivityDate: 2014-12-09T18:50:04.110Z
Answers.LastEditDate: 2014-12-09T18:50:04.110Z
Answers.UserDisplayName: carlcs
Body: <p>I have two levels of product variants in a table field. If the product uses Secondary Variants, I need a way to search the Primary Variants column for unique values, then output individually grouped Secondary variants.</p>
  
  <p>Both Primary and Secondary Variants are unique for each product.</p>
  
  <p>Is there a way to do this with TWIG/Craft templates? If not, what do you recommend? Query JSON? Plug-in?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-12-09T01:45:34.530Z
Comments.Text: Ben, any success using my example code?
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-12-09T18:39:03.900Z
Comments.Text: Apologies for the delay. It had to do with my schedule, not your answer.
Comments.UserDisplayName: Ben
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-12-09T18:45:50.953Z
Comments.Text: No need to apologize, Ben! Just wanted to know if you still use the table or restructured the setup to use some related entries instead. Updated the post :)
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-12-09T18:57:32.883Z
Comments.Text: I'm still using the table for client ease-of-use (as you surmised).
Comments.UserDisplayName: Ben
Tags: <templating><array><table-field>
Title: Index "Column A" for unique values, then recursively group "Column B" values based on those uniques
ThreadText: # Index "Column A" for unique values, then recursively group "Column B" values based on those uniques
  
  (Asked by: username_0 on 2014-12-05)
  
  I have two levels of product variants in a table field. If the product uses Secondary Variants, I need a way to search the Primary Variants column for unique values, then output individually grouped Secondary variants.
  
  Both Primary and Secondary Variants are unique for each product.
  
  Is there a way to do this with TWIG/Craft templates? If not, what do you recommend? Query JSON? Plug-in?
  
  **Tags:** templating, array, table-field
  
  > **Comments:**
  > **username_1**: Ben, any success using my example code?  
  > **username_0**: Apologies for the delay. It had to do with my schedule, not your answer.  
  > **username_1**: No need to apologize, Ben! Just wanted to know if you still use the table or restructured the setup to use some related entries instead. Updated the post :)  
  > **username_0**: I'm still using the table for client ease-of-use (as you surmised).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-12-05. Score: 1 | Accepted answer
  
  Possible, yes. But if you should do it with Twig alone is a totally different thing. There's other ways to do that in Craft,
  e.g. with relations (Entries Fields) and splitting your Products and their variants into separate sections. Or what about using a structure,
  with your variants being child entries to the product?
  
  But here's how you do that with your table field (totally legitimate in my opinion, one advantage being the ease of use for the Client):
  
  ```
  {# Prepare variable before loop #}
  {% set myVariants = [] %}
  
  {% for row in myTableField %}
  
      {# Concatenate a string to the value to prevent it from being numeric #}
      {% set variantKey = 'key' ~ row.col1 %}
  
      {% if variantKey not in myVariants|keys %}
  
          {# Insert a new key and add the first value to it #}
          {% set myVariants = myVariants|merge({ (variantKey): [row.col2] }) %}
  
      {% else %}
  
          {# The key already exists, so merge the value to the existing ones #}
          {% set myVariants = myVariants|merge({ (variantKey): myVariants[variantKey]|merge([row.col2]) }) %}
  
      {% endif %}
  
  {% endfor %}
  
  ```
  
  This is how you print it:
  
  ```
  <ul class="variantsLevel1">
      {% for variantKey, variantsLevel1 in myVariants %}
          <li>
  
              {# Remove the string from our key again #}
              {{ variantKey|slice(3) }}
  
              <ul class="variantsLevel2">
                  {% for variantLevel2 in variantsLevel1 %}
                      <li>{{ variantLevel2 }}</li>
                  {% endfor %}
              </ul>
  
          </li>
      {% endfor %}
  </ul>
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks so much for the response. The only problem I found with using this answer was that by using the word 'key' for the key, it truncated any variantLevel1 names that ended with k,e or y. I'm sure there is a way to force it to only look for that sequence of letters if you would like to edit the answer.  
  
  ---
  
  