<p>The answer is to prepend a letter to the numerical key…</p>
<pre><code>{% for entry in entries %}
    {% set eId   = &quot;e#{entry.id}&quot; %} {# interpolation* #}
    {% set value = entry.intro %}

    {% set myHash = myHash|merge({
        (eId) : { intro : value }
    }) %}
{% endfor %}
</code></pre>
<p>Now the hash stores the custom key as a string…</p>
<pre><code>array(10) {
  [&quot;e1234&quot;]=&gt;
  array(1) {
    [&quot;intro&quot;]=&gt;
    string(13) &quot;My intro text&quot;
…

</code></pre>
<p>…which you can clean up as needed…</p>
<pre><code>{% for key, properties in hash %}
    {% set key = key|replace('e', '') %}
    …
{% endfor %}
</code></pre>
<p>…or factor into lookups…</p>
<pre><code>{% set testId    = 'e1234' %}
{% set testIntro = myHash[testId].intro %}
{# `testIntro`  == 'My intro text' #}
</code></pre>
<p>Apparently, it's <a href="https://twitter.com/CraftCMS/status/1384518340870344705?s=20" rel="nofollow noreferrer">a PHP thing</a>.</p>
<p>*<a href="https://servd.host/blog/10-things-to-not-do-in-your-craft-cms-twig-templates" rel="nofollow noreferrer">Interpolation is often preferable to concatenation</a> &lt;-- see item #4.</p>

CC BY-SA 4.0
2021-04-20T15:00:01.577Z
2021-04-20T15:22:07.710Z
2021-04-20T15:22:07.710Z
Jonathan Schofield
<p>I now know the answer to this but I’m posting it here as a note to myself and in case it helps people.</p>
<p>Let's say you are looping through some entries and storing values in a hash…</p>
<pre><code>{% set myHash = {} %}

{% for entry in entries %}
    {% set value  = entry.intro %}
    {% set myHash = myHash|merge({
        (entry.id) : { intro : value }
    }) %}
{% endfor %}
</code></pre>
<p>The <code>entry.id</code> is a variable so it needs to be wrapped in brackets as a <em>dynamic</em> key.</p>
<p>But when you <code>{{ dump(myHash) }}</code> it turns out the numeric keys have just become index numbers – <code>[0]</code> in the truncated example below…</p>
<pre><code>array(10) {
  [0]=&gt;
  array(1) {
    [&quot;intro&quot;]=&gt;
    string(13) &quot;My intro text&quot;
…
</code></pre>
<p>But there is a straightforward <a href="https://craftcms.stackexchange.com/a/37603/3507">answer</a>…</p>

<hash>
Setting numerical key values in a hash
# Setting numerical key values in a hash

(Asked by: username_0 on 2021-04-20)

I now know the answer to this but I’m posting it here as a note to myself and in case it helps people.
Let's say you are looping through some entries and storing values in a hash…
{% set myHash = {} %}

{% for entry in entries %}
    {% set value  = entry.intro %}
    {% set myHash = myHash|merge({
        (entry.id) : { intro : value }
    }) %}
{% endfor %}

The entry.id is a variable so it needs to be wrapped in brackets as a dynamic key.
But when you {{ dump(myHash) }} it turns out the numeric keys have just become index numbers – [0] in the truncated example below…
array(10) {
  [0]=>
  array(1) {
    ["intro"]=>
    string(13) "My intro text"
…

But there is a straightforward answer…

**Tags:** hash

---

## 1 answers

### Answer by username_0 on 2021-04-20. Score: 2

The answer is to prepend a letter to the numerical key…
{% for entry in entries %}
    {% set eId   = "e#{entry.id}" %} {# interpolation* #}
    {% set value = entry.intro %}

    {% set myHash = myHash|merge({
        (eId) : { intro : value }
    }) %}
{% endfor %}

Now the hash stores the custom key as a string…
array(10) {
  ["e1234"]=>
  array(1) {
    ["intro"]=>
    string(13) "My intro text"
…


…which you can clean up as needed…
{% for key, properties in hash %}
    {% set key = key|replace('e', '') %}
    …
{% endfor %}

…or factor into lookups…
{% set testId    = 'e1234' %}
{% set testIntro = myHash[testId].intro %}
{# `testIntro`  == 'My intro text' #}

Apparently, it's a PHP thing.
*Interpolation is often preferable to concatenation <-- see item #4.


---

