Merge related entries from several matrix blocks while maintaining other matrix field data 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?
Figured it out:

{% for member in allMembers %}
    {% set cat = entry.featuredMembers.relatedTo(member).first() %} 
    {{ member.categoryName }} 
{% endfor %}