小枝 - 建筑阵列中的for循环 [英] twig - building array in for loop
问题描述
是可以反复填充值的树枝阵列?
is it possible to iteratively fill a twig array with values?
{% for question in questions %}
{% set multipleChoiceArray = [] %}
{% for multipleChoice in question.multipleChoiceAnswers %}
{% set multipleChoiceArray = multipleChoiceArray|merge( multipleChoice.answerText ) %}
{% endfor %}
{% endfor %}
问题就在这里 multipleChoiceArray |合并(multipleChoice.answerText)
当我试图传递一个数组例如与主要= loop.index像
when i try to pass an array for example with key = loop.index like
{% set multipleChoiceArray = multipleChoiceArray|merge({"loop['index']":"multipleChoice['answerText']"}) %}
它的工作原理,但该数组包含字符串[循环['指数']:multipleChoice ['answerText']]
it works but the array contains the strings "["loop['index']":"multipleChoice['answerText']"]"
当我试图传递变量,如:
when i try to pass variables like :
{% set multipleChoiceArray = multipleChoiceArray|merge({loop.index:multipleChoice.answerText}) %}
例外是:散列键必须跟一个冒号(:)。意外的标记值标点。 (标点有望与价值:)
exception is : A hash key must be followed by a colon (:). Unexpected token "punctuation" of value "." ("punctuation" expected with value ":")
所以我不能够推值multipleChoice.answerText变成multipleChoiceArray
so i am not able to "push" a value "multipleChoice.answerText" into "multipleChoiceArray"
任何提示如何这是可能的?
我只是想收集所有可能的答案,后来检查,如果答案是阵列中某事计数并显示
any hints how that is possible ? i just want to gather all possible answers and later check if answer is in that array and count sth up and display
推荐答案
合并的参数必须是一个数组或对象与现有的合并。因此,它写成一个元素的数组。
The argument of merge has to be an array or object to merge it with an existing one. So write it as an array with one element.
{% set multipleChoiceAnswerText = multipleChoice.answerText %}
{% set multipleChoiceArray = multipleChoiceArray|merge([multipleChoice.answerText]) %}
这篇关于小枝 - 建筑阵列中的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!