title: PHP - Twig change array key value
body: <p>I am new to Twig and looking for a solution for the following (which would be very easy in PHP, however, our templates are setup in Twig)</p>
  
  <h3>What I'm trying to do</h3>
  
  <p>Edit an array value (using its index) in Twig so that it can be output after a loop.</p>
  
  <h3>What is happening</h3>
  
  <p>The array value (retrieved using its index) does not change when I try to edit the array value by index. Instead, it may append the value to the array</p>
  
  <h3>My code</h3>
  
  <pre><code>...
  {% set amount = [0,0,0] %}
  {% for invoice in invoices %}
  &lt;tr&gt;
      &lt;td&gt;{% if invoice.age &lt;= 10 %}{% set amount ?????? %}{% endif %}&lt;/td&gt;
      &lt;td&gt;{% if invoice.age &gt; 10 and invoice.age &lt;= 20 %}{% set amount ?????? %}{% endif %}&lt;/td&gt;
      &lt;td&gt;{% if invoice.age &gt; 20 %}{% set amount ?????? %}{% endif %}&lt;/td&gt;
  &lt;/tr&gt;
  {% endfor %}
  ...
  {{ amount[0] }}
  </code></pre>
  
  <h3>What I've tried</h3>
  
  <p>I have tried the following to change the value of <code>age[0]</code> to no avail.</p>
  
  <pre><code>{% set amount = amount|merge({0: 'test'}) %}
  
  {% set amount = amount|merge({0: 'test'})|keys %}
  
  {% set amount = amount|merge({(0), 'test'}) %}
  
  {% set amount = amount|merge({(0), 'test'})|keys %}
  
  ... and many more.
  </code></pre>
  
  <h3>Intended outcome</h3>
  
  <p>I want to be able to output <code>{{ age[0] }}</code> at the end to display the amount total of all invoices aged 10 or less. Similarly, I would also like to output <code>age[1]</code> and <code>age[2]</code> to display amount total for all invoices aged between 10 and 20 days, and over 20 days, respectively.</p>