Answers.Body: <p>Figured it out:</p>
  
  <p><code>{% for member in allMembers %}
      {% set cat = entry.featuredMembers.relatedTo(member).first() %} 
      {{ member.categoryName }} 
  {% endfor %}
  </code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-08T17:15:58.210Z
Answers.LastActivityDate: 2017-10-08T20:11:45.553Z
Answers.LastEditDate: 2017-10-08T20:11:45.553Z
Answers.UserDisplayName: artmem
Body: <p>I have a matrix that essentially creates categorized lists of other entries. The fields in my matrix block are "categoryName", a plain text field and "member", a entry relationship field.</p>
  
  <p>I know i can output a list in with all of the blocks in order like this:</p>
  
  <pre><code>{% for block in allMembers %}
    {% for member in block.members %}
      {{ block.categoryName }}:
      {{ member.title|typogrify }}
    {% endfor %}
  {% endfor %}
  </code></pre>
  
  <p>And this give me a list like:</p>
  
  <ul>
  <li>Category 1: Apples </li>
  <li>Category 1: Oranges</li>
  <li>Category 1: Pears</li>
  <li>Category 2: Cats</li>
  <li>Category 2: Dogs</li>
  </ul>
  
  <p>What I really want, though is to combine the contents of both blocks into a single array, and also pull in the category names,so i can sort them alphabetically by related entry title, like this:</p>
  
  <ul>
  <li>Category 1: Apples </li>
  <li>Category 2: Cats</li>
  <li>Category 2: Dogs</li>
  <li>Category 1: Oranges</li>
  <li>Category 1: Pears</li>
  </ul>
  
  <p>I know I can merge all of the related entries into a single array like this:</p>
  
  <pre><code>{% set allMembers = [] %}
  {# loop through all relationship fields for this entry #}
  {% for block in entry.featuredMembers %}
    {# loop through related entries #}
    {% for member in block.members %}
      {# add member to members array #}
      {% set allMembers = allMembers|merge([member]) %}
    {% endfor %}
  {% endfor %}
  
  {% for block in allMembers%}
    {{ block.title }}
  {% endfor %}
  </code></pre>
  
  <p>But then I lose the Category Name. Is there a way to associate the categoryName field with the individual related entries before I merge them into my allMembers array?</p>
  
Tags: <matrix><relations><merge>
Title: Merge related entries from several matrix blocks while maintaining other matrix field data
ThreadText: # Merge related entries from several matrix blocks while maintaining other matrix field data
  
  (Asked by: username_0 on 2017-09-07)
  
  I have a matrix that essentially creates categorized lists of other entries. The fields in my matrix block are "categoryName", a plain text field and "member", a entry relationship field.
  
  I know i can output a list in with all of the blocks in order like this:
  
  ```
  {% for block in allMembers %}
    {% for member in block.members %}
      {{ block.categoryName }}:
      {{ member.title|typogrify }}
    {% endfor %}
  {% endfor %}
  
  ```
  
  And this give me a list like:
  
  * Category 1: Apples
  * Category 1: Oranges
  * Category 1: Pears
  * Category 2: Cats
  * Category 2: Dogs
  
  What I really want, though is to combine the contents of both blocks into a single array, and also pull in the category names,so i can sort them alphabetically by related entry title, like this:
  
  * Category 1: Apples
  * Category 2: Cats
  * Category 2: Dogs
  * Category 1: Oranges
  * Category 1: Pears
  
  I know I can merge all of the related entries into a single array like this:
  
  ```
  {% set allMembers = [] %}
  {# loop through all relationship fields for this entry #}
  {% for block in entry.featuredMembers %}
    {# loop through related entries #}
    {% for member in block.members %}
      {# add member to members array #}
      {% set allMembers = allMembers|merge([member]) %}
    {% endfor %}
  {% endfor %}
  
  {% for block in allMembers%}
    {{ block.title }}
  {% endfor %}
  
  ```
  
  But then I lose the Category Name. Is there a way to associate the categoryName field with the individual related entries before I merge them into my allMembers array?
  
  **Tags:** matrix, relations, merge
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-09-08. Score: 1
  
  Figured it out:
  
  `{% for member in allMembers %}
  {% set cat = entry.featuredMembers.relatedTo(member).first() %}
  {{ member.categoryName }}
  {% endfor %}`
  
  
  ---
  
  