<p>You can use the <a href="http://twig.sensiolabs.org/doc/templates.html#variables" rel="nofollow">subscript syntax</a>:</p>

<p><code>{{ entry['field' ~ loop.index] }}</code></p>

<p>or the <a href="http://twig.sensiolabs.org/doc/functions/attribute.html" rel="nofollow"><code>attribute</code></a> function:</p>

<p><code>{{ attribute(entry, 'field' ~ loop.index) }}</code></p>

<p>The attribute function is more Twig-like in my opinion. But in this case it's only a matter of preference.</p>

<p>Other examples are:</p>

<p><code>{{ myArray[0] }}</code> vs. <code>{{ myArray|first }}</code><br>
<code>{{ myString[0:1] }}</code> vs. <code>{{ myString|slice(0, 1) }}</code></p>

CC BY-SA 3.0
2015-11-07T10:31:04.183Z
2015-11-07T10:42:50.830Z
2015-11-07T10:42:50.830Z
carlcs
<p>Turns out Twig has a nifty little <a href="http://twig.sensiolabs.org/doc/functions/attribute.html" rel="nofollow noreferrer">attribute function</a> that does the trick. Here's how it works:</p>

<pre><code>{% for row in entry.table %}   
   {% set foo = attribute(entry, 'fieldHandle' ~ loop.index) %}
{% endfor %}
</code></pre>

<p>Big hat tip to <a href="https://craftcms.stackexchange.com/users/57/brad-bell">Mr. Brad Bell</a> for posting the original answer in Stack Exchange. </p>

CC BY-SA 3.0
2015-11-06T19:18:59.437Z
You could also just use the [subscript syntax](http://twig.sensiolabs.org/doc/templates.html#variables): `entry['field' ~ loop.index]`.
carlcs
CC BY-SA 3.0
2015-11-06T21:33:34.067Z
@carlcs I thought there might be a simpler way to accomplish that. That works like a charm and, to me, is a more elegant answer. You should post it so I can vote for it!
Brett Burwell
CC BY-SA 3.0
2015-11-06T18:58:47.853Z
2015-11-06T18:58:47.853Z
2017-04-13T12:51:00.253Z
Brett Burwell
<p>I'm looping through the rows of a table field and hoping to target a specific field entry based on the <code>loop.index</code>. Here's a simplified example of what that might look like:</p>

<pre><code>{% for row in entry.table %}   
   {% set foo = entry.field ~ loop.index %}
{% endfor %}
</code></pre>

<p>The doesn't work, but I’m curious if I just have the syntax wrong or if I’m thinking about this the wrong way.</p>

<variable><loop>
Is it possible to use a loop variable to target a specific field entry?
# Is it possible to use a loop variable to target a specific field entry?

(Asked by: username_0 on 2015-11-06)

I'm looping through the rows of a table field and hoping to target a specific field entry based on the loop.index. Here's a simplified example of what that might look like:
{% for row in entry.table %}   
   {% set foo = entry.field ~ loop.index %}
{% endfor %}

The doesn't work, but I’m curious if I just have the syntax wrong or if I’m thinking about this the wrong way.

**Tags:** variable, loop

---

## 2 answers

### Answer by username_1 on 2015-11-07. Score: 1 | Accepted answer

You can use the [subscript syntax](http://twig.sensiolabs.org/doc/templates.html#variables):

`{{ entry['field' ~ loop.index] }}`

or the [`attribute`](http://twig.sensiolabs.org/doc/functions/attribute.html) function:

`{{ attribute(entry, 'field' ~ loop.index) }}`

The attribute function is more Twig-like in my opinion. But in this case it's only a matter of preference.

Other examples are:

`{{ myArray[0] }}` vs. `{{ myArray|first }}`  
`{{ myString[0:1] }}` vs. `{{ myString|slice(0, 1) }}`


---

### Answer by username_0 on 2015-11-06. Score: 1

Turns out Twig has a nifty little attribute function that does the trick. Here's how it works:
{% for row in entry.table %}   
   {% set foo = attribute(entry, 'fieldHandle' ~ loop.index) %}
{% endfor %}

Big hat tip to Mr. Brad Bell for posting the original answer in Stack Exchange.

> **Comments:**
> **username_1**: You could also just use the [subscript syntax](http://twig.sensiolabs.org/doc/templates.html#variables): `entry['field' ~ loop.index]`.  
> **username_0**: @carlcs I thought there might be a simpler way to accomplish that. That works like a charm and, to me, is a more elegant answer. You should post it so I can vote for it!  

---

