<p>Definitely not the most efficient way to do this (it'd be cleaner and more efficient with PHP), but assuming you have  "projects" section that has a "country" Plain Text field and it also had a "state" Plain Text field and you wanted to group projects by country and then state, something like this should work:</p>

<pre><code>{% set countryArr = {} %}

{# grab all projects #}
{% set allEntries = craft.entries.section('projects').limit(null) %}

{# group them by country #}
{% set countries = allEntries|group('country') %}

{# Build the nested array structure #}
{% for country, projects in countries %}
    {# if it's a new country, add it to our structure #}
    {% if countryArr[country] is not defined %}
        {% set countryArr = countryArr|merge({(country):[]}) %}
    {% endif %}

    {# loop through all of the projects in that country #}
    {% for project in projects %}
        {# see if we have a record for the state yet #}
        {% if project.state not in countryArr[country] %}
            {# because twig isn't great with multidimensional arrays #}
            {% set tempArr = countryArr[country] %}
            {% set tempArr = tempArr|merge([project.state]) %}

            {# merge back into our main structure indexed by country #}
            {% set countryArr = countryArr|merge({(country):tempArr})  %}
        {% endif %}
    {% endfor %}
{% endfor %}

{# output our structure #}
{% for country, projects in countryArr %}
    &lt;ul&gt;
        &lt;li&gt;{{ country }}
            &lt;ul&gt;
                {% for project in projects %}
                    &lt;li&gt;{{ project }}&lt;/li&gt;
                {% endfor %}
            &lt;/ul&gt;
        &lt;/ul&gt;
    &lt;/li&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2018-04-27T21:11:59.847Z
2018-04-27T21:11:59.847Z
Brad Bell
<p>I'm trying to create a list of countries with a sublist of territories per country based on their existence in the same record. The following is shorthand pseudocode to illustrate:</p>

<pre><code>    locations =&gt;
         United States =&gt;
             California
             Massachusettes
             New York
         France =&gt;
             Lille
             Marseille
</code></pre>

<p>Here is my code so far:</p>

<pre><code>   {% set allEntries = craft.entries.section('projects').limit(2000) %}
   {% set countries = allEntries|group('country') %}
   {% set country_arr ={} %}

   {% for country, entriesInCountry in countries %}

       {% if not attribute(country_arr, country) is defined %}

         {# Add country to list, if unique #}

         {% set country_arr = country_arr | merge([country]) %} 

       {% endif %}

       {% for entry in entriesInCountry %}

          {# Somehow add the entry.state here as a property or sub-object of the country, if unique #}

       {% endfor %}
  {% endfor %}
</code></pre>

CC BY-SA 3.0
2018-04-19T21:50:38.907Z
Why don't you just relate your territories with your countries via relation field or you create a structured section with parent child relations
Robin Schambach
CC BY-SA 3.0
2018-04-19T21:52:33.087Z
The data for the site self-presented in this way and it's how it makes sense to the client. I'm trying to avoid redundant queries in this instance and a few other relational instances
Caroline from Aquarian
CC BY-SA 3.0
2018-04-19T21:54:12.700Z
There won't be redundant queries when you use relation fields. How to you know which territory belongs to which country then?
Robin Schambach
CC BY-SA 3.0
2018-04-19T21:56:23.903Z
I have roughly 1,620 records of these projects, some data being geographical. The client needs to have the freedom to add or edit these records without going into field setup. They set it up as text, and it appears in search drop downs on the next query.
Caroline from Aquarian
CC BY-SA 3.0
2018-04-19T21:59:47.623Z
Well without explaining more of your structure you won't receive any help since we can't provide any hints. You have a custom record and don't use Craft elements, you don't use Craft fields but how are your records stored. Could you show us both classes?
Robin Schambach
CC BY-SA 3.0
2018-04-19T22:03:50.320Z
I don't know how to answer this.
Caroline from Aquarian
CC BY-SA 3.0
2018-04-24T03:40:54.150Z
Can you edit the original question and share the fields (with the names and types) in the section "projects"? Having a hard time seeing how countries and states and being represented in that section.
Brad Bell
<templating>
Create structured object from query results
# Create structured object from query results

(Asked by: username_0 on 2018-04-19)

I'm trying to create a list of countries with a sublist of territories per country based on their existence in the same record. The following is shorthand pseudocode to illustrate:
    locations =>
         United States =>
             California
             Massachusettes
             New York
         France =>
             Lille
             Marseille

Here is my code so far:
   {% set allEntries = craft.entries.section('projects').limit(2000) %}
   {% set countries = allEntries|group('country') %}
   {% set country_arr ={} %}

   {% for country, entriesInCountry in countries %}

       {% if not attribute(country_arr, country) is defined %}

         {# Add country to list, if unique #}

         {% set country_arr = country_arr | merge([country]) %} 

       {% endif %}

       {% for entry in entriesInCountry %}

          {# Somehow add the entry.state here as a property or sub-object of the country, if unique #}

       {% endfor %}
  {% endfor %}

**Tags:** templating
> **Comments:**
> **username_1**: Why don't you just relate your territories with your countries via relation field or you create a structured section with parent child relations  
> **username_0**: The data for the site self-presented in this way and it's how it makes sense to the client. I'm trying to avoid redundant queries in this instance and a few other relational instances  
> **username_1**: There won't be redundant queries when you use relation fields. How to you know which territory belongs to which country then?  
> **username_0**: I have roughly 1,620 records of these projects, some data being geographical. The client needs to have the freedom to add or edit these records without going into field setup. They set it up as text, and it appears in search drop downs on the next query.  
> **username_1**: Well without explaining more of your structure you won't receive any help since we can't provide any hints. You have a custom record and don't use Craft elements, you don't use Craft fields but how are your records stored. Could you show us both classes?  
> **username_0**: I don't know how to answer this.  
> **username_2**: Can you edit the original question and share the fields (with the names and types) in the section "projects"? Having a hard time seeing how countries and states and being represented in that section.  

---

## 1 answers

### Answer by username_2 on 2018-04-27. Score: 1 | Accepted answer

Definitely not the most efficient way to do this (it'd be cleaner and more efficient with PHP), but assuming you have  "projects" section that has a "country" Plain Text field and it also had a "state" Plain Text field and you wanted to group projects by country and then state, something like this should work:
{% set countryArr = {} %}

{# grab all projects #}
{% set allEntries = craft.entries.section('projects').limit(null) %}

{# group them by country #}
{% set countries = allEntries|group('country') %}

{# Build the nested array structure #}
{% for country, projects in countries %}
    {# if it's a new country, add it to our structure #}
    {% if countryArr[country] is not defined %}
        {% set countryArr = countryArr|merge({(country):[]}) %}
    {% endif %}

    {# loop through all of the projects in that country #}
    {% for project in projects %}
        {# see if we have a record for the state yet #}
        {% if project.state not in countryArr[country] %}
            {# because twig isn't great with multidimensional arrays #}
            {% set tempArr = countryArr[country] %}
            {% set tempArr = tempArr|merge([project.state]) %}

            {# merge back into our main structure indexed by country #}
            {% set countryArr = countryArr|merge({(country):tempArr})  %}
        {% endif %}
    {% endfor %}
{% endfor %}

{# output our structure #}
{% for country, projects in countryArr %}
    
        {{ country }}
            
                {% for project in projects %}
                    {{ project }}
                {% endfor %}
            
        
    
{% endfor %}


---

