<p>You are setting <code>option</code> to a string here:</p>

<pre><code>{% set option %}...{% endset %}
</code></pre>

<p>And <a href="http://twig.sensiolabs.org/doc/filters/merge.html" rel="nofollow">merge</a> takes an array or a hash as a parameter. It looks to me like you want <code>option</code> to be a hash:</p>

<pre><code>{% set option = {factor: {'factorVal': factor, 'factorLbl': factorLbl}} %} 
</code></pre>

CC BY-SA 3.0
2015-12-23T13:45:54.987Z
Ahh, good catch. This gets rid of the error, however, the `merge` is not working as it only has one key. Adding  `{{ dump(option) }}` within the `for` loop prints each key. I should have a total of 4 keys within the hash.
Siebird
CC BY-SA 3.0
2015-12-22T22:33:11.357Z
2015-12-22T22:33:11.357Z
Marion Newlevant
<p>Marion got me pointed in the right direction. I was setting <code>factor</code> as a string in my original post, which only allowed one key in the hash as it was 'seen' as the same every time through the <code>merge</code> function in the for loop. Converting <code>factor</code> to a variable fixed this:</p>

<pre><code>{% set contentTypes = {} %}

{% for row in survey.questions %}
    {% set settings = row.settings.first %}
    {% set factor = settings.factor ~ (settings.subFactor ? '_' ~ settings.subFactor) %}
    {% set factorLbl = factor|title|replace('_', ' / ') %}
    {% set contentTypes = contentTypes|merge({ (factor) : {'value': factor, 'label': factorLbl} }) %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-12-23T15:32:36.807Z
2015-12-23T15:32:36.807Z
Siebird
<p>I'm having issues trying to <code>merge</code> a nested hash to get unique values. I'm getting this error which points to the last line of for the loop:</p>

<p><code>A hash key must be followed by a colon (:). Unexpected token "punctuation" of value "}" ("punctuation" expected with value ":").</code></p>

<pre><code>{% set contentTypes = {} %}

{% for row in survey.questions %}
    {% set settings = row.settings.first %}
    {% set factor %}{{ settings.factor }}{{ settings.subFactor ? '_' ~ settings.subFactor }}{% endset %}
    {% set factorLbl = factor|title|replace('_', ' / ') %}
    {% set option %}{{ factor }}: { factorVal: '{{ factor }}', factorLbl: '{{ factorLbl }}'}{% endset %}
    {% set contentTypes = contentTypes|merge({option}) %}
{% endfor %}
</code></pre>

<p><strong>Update</strong></p>

<p>So <code>factor</code> is currently being set as a string, which is causing the <code>merge</code> to only set the last key in the for loop. How do I concatenate the two strings into a variable if <code>settings.subFactor</code> exists?</p>

<p><code>{% set factor = settings.factor ~ settings.subFactor ? '_' ~ settings.subFactor %}</code></p>

<merge>
Merging Hash Error
# Merging Hash Error

(Asked by: username_0 on 2015-12-22)

I'm having issues trying to merge a nested hash to get unique values. I'm getting this error which points to the last line of for the loop:
A hash key must be followed by a colon (:). Unexpected token "punctuation" of value "}" ("punctuation" expected with value ":").
{% set contentTypes = {} %}

{% for row in survey.questions %}
    {% set settings = row.settings.first %}
    {% set factor %}{{ settings.factor }}{{ settings.subFactor ? '_' ~ settings.subFactor }}{% endset %}
    {% set factorLbl = factor|title|replace('_', ' / ') %}
    {% set option %}{{ factor }}: { factorVal: '{{ factor }}', factorLbl: '{{ factorLbl }}'}{% endset %}
    {% set contentTypes = contentTypes|merge({option}) %}
{% endfor %}

Update
So factor is currently being set as a string, which is causing the merge to only set the last key in the for loop. How do I concatenate the two strings into a variable if settings.subFactor exists?
{% set factor = settings.factor ~ settings.subFactor ? '_' ~ settings.subFactor %}

**Tags:** merge

---

## 2 answers

### Answer by username_1 on 2015-12-22. Score: 3

You are setting option to a string here:
{% set option %}...{% endset %}

And merge takes an array or a hash as a parameter. It looks to me like you want option to be a hash:
{% set option = {factor: {'factorVal': factor, 'factorLbl': factorLbl}} %}

> **Comments:**
> **username_0**: Ahh, good catch. This gets rid of the error, however, the `merge` is not working as it only has one key. Adding  `{{ dump(option) }}` within the `for` loop prints each key. I should have a total of 4 keys within the hash.  

---

### Answer by username_0 on 2015-12-23. Score: 0

Marion got me pointed in the right direction. I was setting factor as a string in my original post, which only allowed one key in the hash as it was 'seen' as the same every time through the merge function in the for loop. Converting factor to a variable fixed this:
{% set contentTypes = {} %}

{% for row in survey.questions %}
    {% set settings = row.settings.first %}
    {% set factor = settings.factor ~ (settings.subFactor ? '_' ~ settings.subFactor) %}
    {% set factorLbl = factor|title|replace('_', ' / ') %}
    {% set contentTypes = contentTypes|merge({ (factor) : {'value': factor, 'label': factorLbl} }) %}
{% endfor %}


---

