<p>You'd actually need to retrieve all of the entries to count them in this way. Once you've got the complete collection, it would simply be a matter of looping through them and adding everything up.</p>
<p><strong>1. Get all entries in California.</strong></p>
<pre><code>{% set params = {
    filter: {
        state: 'CA'
    }
} %}

{% set meetings = craft.entries.address(params).all() %}
</code></pre>
<p><strong>2. Loop through those entries. Build a new array based on the results.</strong></p>
<pre><code>{% set stateMeetings = {} %}
    
{% for meeting in meetings %}

    {% set city = meeting.address.city %}

    {% if stateMeetings[city] is defined %}
        {% set cityTotal = stateMeetings[city] + 1 %}
    {% else %}
        {% set cityTotal = 1 %}
    {% endif %}

    {% set stateMeetings = stateMeetings|merge{(city):cityTotal} %}

{% endfor %}
</code></pre>
<p><strong>3. By the time that's all done, you'll have a neat associative array, where each key is a different city, and the value of each represents how many times it was found.</strong></p>
<pre><code>{
    &quot;Los Angeles&quot;: 3,
    &quot;San Diego&quot;: 6,
    &quot;Modesto&quot;: 1,
}
</code></pre>
<p>You'll definitely want to cache all of that... it'll be a pretty expensive process.</p>
<p>To be honest, I'd actually recommend doing all of this work in a plugin (or module). It's a fairly easy thing to achieve with a <strong>template hook</strong>. This kind of heavy merging is ill-fitted for Twig, but much simpler in PHP.</p>

CC BY-SA 3.0
2018-03-20T11:52:52.600Z
Thanks. Yeah, I am learning plugins now. Pretty easy to do with sql directly in the table. Haven't looked at modules yet at all.
Eric Snyder
CC BY-SA 4.0
2018-03-20T04:43:08.033Z
2021-01-01T07:03:45.510Z
2021-01-01T07:03:45.510Z
Lindsey D
<p>Using the following filter will return all the entries for 'CA'. </p>

<pre><code>{% set params = {
    filter: {
        state: 'CA'
    }
} %}

{% set meetings = craft.entries.address(params) %}
</code></pre>

<p>Let's say that I get 10 returns for 'CA':</p>

<pre><code>Los Angeles
Los Angeles
Los Angeles
San Diego
San Diego
San Diego
San Diego
San Diego
San Diego
Modesto
</code></pre>

<p>How would I go about one getting 3 results returned for each unique value for City? I only want to list each city once rather than 3 Los Angeles, 16 San Diego's and 1 Modesto. Like this:</p>

<pre><code>Los Angeles
San Diego
Modesto
</code></pre>

<p>It could also be helpful to have them like this:</p>

<pre><code>3 Los Angeles
6 San Diego
1 Modesto
</code></pre>

<p>I am building a navigation by state with a list of cities under each state. To look like this:</p>

<pre><code>CA
   Los Angeles
   San Diego
   Modesto
KS
   Wichita
   Kansas City
</code></pre>

<craft3><plugin-smartmap>
Getting unique values from Smart Map addresses
# Getting unique values from Smart Map addresses

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

Using the following filter will return all the entries for 'CA'. 
{% set params = {
    filter: {
        state: 'CA'
    }
} %}

{% set meetings = craft.entries.address(params) %}

Let's say that I get 10 returns for 'CA':
Los Angeles
Los Angeles
Los Angeles
San Diego
San Diego
San Diego
San Diego
San Diego
San Diego
Modesto

How would I go about one getting 3 results returned for each unique value for City? I only want to list each city once rather than 3 Los Angeles, 16 San Diego's and 1 Modesto. Like this:
Los Angeles
San Diego
Modesto

It could also be helpful to have them like this:
3 Los Angeles
6 San Diego
1 Modesto

I am building a navigation by state with a list of cities under each state. To look like this:
CA
   Los Angeles
   San Diego
   Modesto
KS
   Wichita
   Kansas City

**Tags:** craft3, plugin-smartmap


---

## 1 answers

### Answer by username_1 on 2018-03-20. Score: 3 | Accepted answer

You'd actually need to retrieve all of the entries to count them in this way. Once you've got the complete collection, it would simply be a matter of looping through them and adding everything up.
1. Get all entries in California.
{% set params = {
    filter: {
        state: 'CA'
    }
} %}

{% set meetings = craft.entries.address(params).all() %}

2. Loop through those entries. Build a new array based on the results.
{% set stateMeetings = {} %}
    
{% for meeting in meetings %}

    {% set city = meeting.address.city %}

    {% if stateMeetings[city] is defined %}
        {% set cityTotal = stateMeetings[city] + 1 %}
    {% else %}
        {% set cityTotal = 1 %}
    {% endif %}

    {% set stateMeetings = stateMeetings|merge{(city):cityTotal} %}

{% endfor %}

3. By the time that's all done, you'll have a neat associative array, where each key is a different city, and the value of each represents how many times it was found.
{
    "Los Angeles": 3,
    "San Diego": 6,
    "Modesto": 1,
}

You'll definitely want to cache all of that... it'll be a pretty expensive process.
To be honest, I'd actually recommend doing all of this work in a plugin (or module). It's a fairly easy thing to achieve with a template hook. This kind of heavy merging is ill-fitted for Twig, but much simpler in PHP.

> **Comments:**
> **username_0**: Thanks. Yeah, I am learning plugins now. Pretty easy to do with sql directly in the table. Haven't looked at modules yet at all.  

---

