0

I have a problem with an array of arrays in Twig. Here is the code I am struggling with :

{% set tabTmp = {0:{},1:{},2:{},3:{},4:{},5:{},6:{},7:{},8:{}} %}
{%for element in box.elements%}
  {% set tab = tabTmp[element.category.id] %}
  {% set elementId = element.id %}
  {% set tab = tab | merge({elementId:element}) %}
{% endfor%}

{%for key, tmp in tabTmp %}
  {% if tmp is iterable %}
     {{ dump(tmp) }}
  {% endif %}
{% endfor%}             

box.elements and element exist, element.category.id and element.id are integer and element is the object I want to work with.

But I keep having Array(0) as a result of dump(tmp).

Any ideas ?

1 Answer 1

3

Everything looks fine, but if you want to merge a variable as key to an associative array you need to use ();
so try changing

{% set tab = tab | merge({elementId:element}) %}

To

{% set tab = tab | merge({(elementId):element}) %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.