<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>

CC BY-SA 3.0
2014-12-09T18:37:13.823Z
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.
Ben
CC BY-SA 3.0
2014-12-05T23:15:34.353Z
2014-12-09T18:50:04.110Z
2014-12-09T18:50:04.110Z
carlcs
<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>

CC BY-SA 3.0
2014-12-09T01:45:34.530Z
Ben, any success using my example code?
carlcs
CC BY-SA 3.0
2014-12-09T18:39:03.900Z
Apologies for the delay. It had to do with my schedule, not your answer.
Ben
CC BY-SA 3.0
2014-12-09T18:45:50.953Z
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 :)
carlcs
CC BY-SA 3.0
2014-12-09T18:57:32.883Z
I'm still using the table for client ease-of-use (as you surmised).
Ben
<templating><array><table-field>
Index "Column A" for unique values, then recursively group "Column B" values based on those uniques
# 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**: username_0, 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, username_0! 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:

    {% for variantKey, variantsLevel1 in myVariants %}
        

            {# Remove the string from our key again #}
            {{ variantKey|slice(3) }}

            
                {% for variantLevel2 in variantsLevel1 %}
                    {{ variantLevel2 }}
                {% endfor %}
            

        
    {% endfor %}

> **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.  

---

