Answers.Body: <p>Yep, there's a great mystery about how the <code>in</code> containment operator actually works with objects. <a href="http://twig.sensiolabs.org/doc/templates.html#containment-operator" rel="noreferrer">Twig Docs say</a> it's possible:</p>
  
  <blockquote>
    <p>You can use this filter to perform a containment test on strings, arrays, or objects implementing the Traversable interface.</p>
  </blockquote>
  
  <p>but I also tried this (multiple times) and did't get it working. I eventually always ended up using a workaround. In your case I'd do the following and use the entry's ID in the conditional:</p>
  
  <pre><code>{# Get arrays of entries #}
  {% set entriesA = craft.entries(paramsA).find() %}
  {% set entriesB = craft.entries(paramsB).find() %}
  
  {# Prepare merge #}
  {% set entriesBIds = craft.entries(paramsB).ids() %}
  {% set allEntries = entriesB %}
  
  {# Merge arrays #}
  {% for entry in entriesA %}
  
      {# Don't merge duplicates #}
      {% if entry.id not in entriesBIds %}
          {% set allEntries = allEntries|merge([entry]) %}
      {% endif %}
  
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-31T13:59:20.347Z
Answers.Text: Thanks for this. I ended up doing something very similar.
Answers.UserDisplayName: hamishtaplin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-31T17:22:34.310Z
Answers.Text: The problem with "in" and objects is that since an object can have an variable depth, how deep does it need to check before deciding if they two objects are equal? Is a "shallow comparison" good enough, where it just compares the properties/methods on the first level, or do you do a much more expensive "deep comparison" and explore the whole object tree before deciding?
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-31T12:39:33.997Z
Answers.LastActivityDate: 2014-10-31T12:39:33.997Z
Answers.UserDisplayName: carlcs
Body: <p>I have two queries on related entries (using Many to Many plugin) to find both related and then reverse related entries:</p>
  
  
  
  <pre class="lang-twig prettyprint-override"><code>{% set relatedArticles = article.relatedArticles %}
  
  {% set reverseRelatedArticles = craft.entries.relatedTo(article) %}`
  </code></pre>
  
  <p>I want to merge these two ElementCriteriaModel, removing any duplicates before processing them further. I've tried this:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% for item in reverseRelatedArticles %}
      {% if item not in relatedArticles %}
          {% set relatedArticles = relatedArticles|merge([item]) %}
      {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>But it doesn't remove the duplicates. Adding <code>find()</code> to the queries to turn them in to an array doesn't work either. Any way I can do this?</p>
  
  <p>EDIT: I tried this, too:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set relatedArticles = article.relatedArticles.find()|merge(craft.entries.relatedTo(article).find()) %}
  </code></pre>
  
Tags: <templating><entries>
Title: Merge two arrays of ElementModels?
ThreadText: # Merge two arrays of ElementModels?
  
  (Asked by: username_0 on 2014-10-31)
  
  I have two queries on related entries (using Many to Many plugin) to find both related and then reverse related entries:
  
  ```twig
  {% set relatedArticles = article.relatedArticles %}
  
  {% set reverseRelatedArticles = craft.entries.relatedTo(article) %}`
  
  ```
  
  I want to merge these two ElementCriteriaModel, removing any duplicates before processing them further. I've tried this:
  
  ```twig
  {% for item in reverseRelatedArticles %}
      {% if item not in relatedArticles %}
          {% set relatedArticles = relatedArticles|merge([item]) %}
      {% endif %}
  {% endfor %}
  
  ```
  
  But it doesn't remove the duplicates. Adding `find()` to the queries to turn them in to an array doesn't work either. Any way I can do this?
  
  EDIT: I tried this, too:
  
  ```twig
  {% set relatedArticles = article.relatedArticles.find()|merge(craft.entries.relatedTo(article).find()) %}
  
  ```
  
  **Tags:** templating, entries
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-10-31. Score: 7 | Accepted answer
  
  Yep, there's a great mystery about how the `in` containment operator actually works with objects. [Twig Docs say](http://twig.sensiolabs.org/doc/templates.html#containment-operator) it's possible:
  
  > You can use this filter to perform a containment test on strings, arrays, or objects implementing the Traversable interface.
  
  but I also tried this (multiple times) and did't get it working. I eventually always ended up using a workaround. In your case I'd do the following and use the entry's ID in the conditional:
  
  ```
  {# Get arrays of entries #}
  {% set entriesA = craft.entries(paramsA).find() %}
  {% set entriesB = craft.entries(paramsB).find() %}
  
  {# Prepare merge #}
  {% set entriesBIds = craft.entries(paramsB).ids() %}
  {% set allEntries = entriesB %}
  
  {# Merge arrays #}
  {% for entry in entriesA %}
  
      {# Don't merge duplicates #}
      {% if entry.id not in entriesBIds %}
          {% set allEntries = allEntries|merge([entry]) %}
      {% endif %}
  
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for this. I ended up doing something very similar.  
  > **username_2**: The problem with "in" and objects is that since an object can have an variable depth, how deep does it need to check before deciding if they two objects are equal? Is a "shallow comparison" good enough, where it just compares the properties/methods on the first level, or do you do a much more expensive "deep comparison" and explore the whole object tree before deciding?  
  
  ---
  
  