<p>If you need only unique letters, you can build a new array and only merge first letters that are not already in the array like this:</p>

<pre><code>{% set letterArray = [] %}

{% for alpha in craft.entries.section('advertisers').order('title asc') %}
  {% if alpha.title|first not in letterArray %}
    {% set letterArray = letterArray|merge([alpha.title|first]) %}
  {% endif %}
{% endfor %}

{% for letter in letterArray %}
  &lt;a href="{{ siteUrl }}{{ letter }}"&gt;{{ letter }}&lt;/a&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-04-06T20:28:54.000Z
2017-04-06T20:28:54.000Z
Alex Roper
<p>Completely untested, but something like this should work:</p>

<pre><code>{% set letterArray = [] %}

{% for alpha in craft.entries.section('advertisers').order('title asc') %}
    {% set letterArray = letterArray|merge([alpha.title|first]) %}
{% endfor %}

{% for letter in letterArray %}
    &lt;a href="{{ siteUrl }}{{ letter }}"&gt;{{ letter }}&lt;/a&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-04-06T01:12:23.277Z
I am getting this error: "The merge filter only works with arrays or "Traversable", got "string" as second argument."
afc
CC BY-SA 3.0
2017-04-06T02:51:03.340Z
I did say it wasn't tested... try the updated answer.
Brad Bell
CC BY-SA 3.0
2017-04-08T01:00:59.150Z
No error but still returns the same as my original code. The suggestion by Alex worked perfect. I appreciate your assistance!
afc
CC BY-SA 3.0
2017-04-08T03:36:31.107Z
Foiled by @AlexRoper yet again!
Brad Bell
CC BY-SA 3.0
2017-04-03T02:57:08.077Z
2017-04-06T02:50:46.600Z
2017-04-06T02:50:46.600Z
Brad Bell
<p>I've got a for loop that is returning the first letter of the entry title. I have a number of entries so I almost get a complete alphabet except that there are too many of many letters. I only want one 'a', one 'b', one 'c' and so 
forth. And no letter if there is no entry to correspond to it.</p>

<pre><code>{% for alpha in craft.entries.section('advertisers').order('title asc') %}
    &lt;a href="{{ siteUrl }}{{ alpha.title|first }}"&gt;{{ alpha.title|first }}&lt;/a&gt;
{% endfor %}
</code></pre>

<p>This code returns:
A A A B B B B B C C E E E E G G H H K M M M M P P R R S S S T T U U U W W
but I want it to return A B C E G H K M P R S T U W</p>

<p>Are there any solutions to this?</p>

CC BY-SA 3.0
2017-03-31T23:08:13.653Z
Correct me if I'm wrong, but the only code relevant to your question is the first for loop, right?
Brad Bell
CC BY-SA 3.0
2017-03-31T23:21:16.817Z
yup. I removed some code to clean it up. I'm still learning the ways of Stackexchange.
afc
<forloop>
How do I omit similar content in a for loop based on the first letter in title?
# How do I omit similar content in a for loop based on the first letter in title?

(Asked by: username_0 on 2017-03-31)

I've got a for loop that is returning the first letter of the entry title. I have a number of entries so I almost get a complete alphabet except that there are too many of many letters. I only want one 'a', one 'b', one 'c' and so 
forth. And no letter if there is no entry to correspond to it.
{% for alpha in craft.entries.section('advertisers').order('title asc') %}
    {{ alpha.title|first }}
{% endfor %}

This code returns:
A A A B B B B B C C E E E E G G H H K M M M M P P R R S S S T T U U U W W
but I want it to return A B C E G H K M P R S T U W
Are there any solutions to this?

**Tags:** forloop

> **Comments:**
> **username_1**: Correct me if I'm wrong, but the only code relevant to your question is the first for loop, right?  
> **username_0**: yup. I removed some code to clean it up. I'm still learning the ways of Stackexchange.  

---

## 2 answers

### Answer by username_2 on 2017-04-06. Score: 2 | Accepted answer

If you need only unique letters, you can build a new array and only merge first letters that are not already in the array like this:
{% set letterArray = [] %}

{% for alpha in craft.entries.section('advertisers').order('title asc') %}
  {% if alpha.title|first not in letterArray %}
    {% set letterArray = letterArray|merge([alpha.title|first]) %}
  {% endif %}
{% endfor %}

{% for letter in letterArray %}
  {{ letter }}
{% endfor %}


---

### Answer by username_1 on 2017-04-03. Score: 1

Completely untested, but something like this should work:
{% set letterArray = [] %}

{% for alpha in craft.entries.section('advertisers').order('title asc') %}
    {% set letterArray = letterArray|merge([alpha.title|first]) %}
{% endfor %}

{% for letter in letterArray %}
    {{ letter }}
{% endfor %}

> **Comments:**
> **username_0**: I am getting this error: "The merge filter only works with arrays or "Traversable", got "string" as second argument."  
> **username_1**: I did say it wasn't tested... try the updated answer.  
> **username_0**: No error but still returns the same as my original code. The suggestion by Alex worked perfect. I appreciate your assistance!  
> **username_1**: Foiled by @AlexRoper yet again!  

---

