3

I have these to arrays that I need to merge into one array, but can't figure it out.

Please advise

{% set entries = craft.entries.section('publications').search('nonMembers:0') %}

{% set entries = craft.entries.section('improveYourClub').search('membersExclusive:1') %}

1 Answer 1

5

Just a few side notes:

  1. neither the first entries variable nor the second entries variable is an array - both are objects of kind craft\elements\db\EntryQuery (or craft\ElementCriteriaModel if it's Craft 2).
  2. .search('nonMembers:0') can be shortened to nonMembers(0) (or "0" or "not 1" depending on the type) and search('membersExclusive:1') to membersExclusive(1) it will increase your performance as well

So far about that.. You can merge arrays in Twig via |merge filter

{% set values = [1, 2] %}

{% set values = values|merge(['apple', 'orange']) %}

{# values now contains [1, 2, 'apple', 'orange'] #}

EDIT:

To address your case specifically, you'll need something like this...

{% set entriesOne = craft.entries.section('publications').search('nonMembers:0').find() %}
{% set entriesTwo = craft.entries.section('improveYourClub').search('membersExclusive:1').find() %}

{% set allEntries = entriesOne|merge(entriesTwo) %}
7
  • In that case how do I merge the two objects
    – CreateSean
    Commented Aug 27, 2018 at 17:24
  • 1
    You can't merge objects. You can only merge the results of both queries Commented Aug 27, 2018 at 17:27
  • @CreateSean What Robin is saying is, add a .find() to the end of both queries. Then you'll have arrays of elements (instead of ElementCriteriaModels).
    – Lindsey D
    Commented Aug 27, 2018 at 19:41
  • adding .find() returns a php notice with an Array to string conversion error. I had already tried that.
    – CreateSean
    Commented Aug 28, 2018 at 17:49
  • @CreateSean Just edited Robin's answer to use your specific code... give that a shot.
    – Lindsey D
    Commented Aug 28, 2018 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.