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') %}
4

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) %}
  • In that case how do I merge the two objects – CreateSean Aug 27 '18 at 17:24
  • You can't merge objects. You can only merge the results of both queries – Robin Schambach Aug 27 '18 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 Aug 27 '18 at 19:41
  • adding .find() returns a php notice with an Array to string conversion error. I had already tried that. – CreateSean Aug 28 '18 at 17:49
  • @CreateSean Just edited Robin's answer to use your specific code... give that a shot. – Lindsey D Aug 28 '18 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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