<p>Holy moly! I've solved it already, Batman. Check the array before adding the event to it, like so:</p>

<pre><code>{% if event not in categoryEvents %}
        {% set categoryEvents = categoryEvents | merge([event]) %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-01-07T12:22:30.610Z
2015-01-07T12:22:30.610Z
Clive Portman
<p>I have some matrix fields containing a date. I then loop through the entries in a category looking for entries containing a matrix field with a date during the next month. Everytime a matrix field is found, the entry id is added to an array of entries. Like so:</p>

<pre><code>{% for event in craft.entries.section('events').type('eventWithMultipleDays').relatedTo(category).find() %}
    {% for date in event.dates if date.multiDate &gt;= now | date("Y-m-d") and date.multiDate &lt;= now | date_modify('+1 month') %}
        {# ADD THE FILTERED EVENT TO THE CATEGORYSEVENTS ARRAY #}
        {# NOTE SQUARE BRACKETS CREATE AN ARRAY - NECESSARY FOR THE MERGE #}
        {% set categoryEvents = categoryEvents | merge([event]) %}          
    {% endfor %}
{% endfor %}
</code></pre>

<p>Not surprisingly, some entries contain several matrix blocks with dates falling within the next month, which means the array is containing duplicate entries. That means when I output the entries array, I'm getting repetition, using this, as you'd expect:</p>

<pre><code>{% for event in categoryEvents %}
    {# details output #}
{% endfor %}
</code></pre>

<p>Is there a way to only get unique values out of this array? Or, if not, a better way to create the array perhaps?</p>

<p>Thanks,
Clive</p>

<matrix><datetime><array>
Filter out duplicate entries in array?
# Filter out duplicate entries in array?

(Asked by: username_0 on 2015-01-07)

I have some matrix fields containing a date. I then loop through the entries in a category looking for entries containing a matrix field with a date during the next month. Everytime a matrix field is found, the entry id is added to an array of entries. Like so:
{% for event in craft.entries.section('events').type('eventWithMultipleDays').relatedTo(category).find() %}
    {% for date in event.dates if date.multiDate >= now | date("Y-m-d") and date.multiDate <= now | date_modify('+1 month') %}
        {# ADD THE FILTERED EVENT TO THE CATEGORYSEVENTS ARRAY #}
        {# NOTE SQUARE BRACKETS CREATE AN ARRAY - NECESSARY FOR THE MERGE #}
        {% set categoryEvents = categoryEvents | merge([event]) %}          
    {% endfor %}
{% endfor %}

Not surprisingly, some entries contain several matrix blocks with dates falling within the next month, which means the array is containing duplicate entries. That means when I output the entries array, I'm getting repetition, using this, as you'd expect:
{% for event in categoryEvents %}
    {# details output #}
{% endfor %}

Is there a way to only get unique values out of this array? Or, if not, a better way to create the array perhaps?
Thanks,
Clive

**Tags:** matrix, datetime, array


---

## 1 answers

### Answer by username_0 on 2015-01-07. Score: 4 | Accepted answer

Holy moly! I've solved it already, Batman. Check the array before adding the event to it, like so:
{% if event not in categoryEvents %}
        {% set categoryEvents = categoryEvents | merge([event]) %}
{% endif %}


---

