# How do I get the second Item in an Iteration?

**URL:** <https://forum.shopware.com/t/how-do-i-get-the-second-item-in-an-iteration/40446>\
**Category:** Programmierung\
**Created:** [11. Oktober 2016 um 10:31 UTC](https://forum.shopware.com/t/how-do-i-get-the-second-item-in-an-iteration/40446 "2016-10-11T10:31:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex220x](https://avatars.discourse-cdn.com/v4/letter/a/ccd318/32.png) [@alex220x](https://forum.shopware.com/u/alex220x)\
**Post date:** [11. Oktober 2016 um 10:31 UTC](https://forum.shopware.com/t/how-do-i-get-the-second-item-in-an-iteration/40446/1 "2016-10-11T10:31:32Z")

</div>

&nbsp; **I get the first item like this:**

```
{foreach $sArticle.sBlockPrices as $blockPrice}
  {if $blockPrice@first}
    First element
  {else}
    ...
  {/if}
{/foreach}

```

How do I get the second/third item?&nbsp;

---

<div class="post-metadata">

**Author:** ![EikeBrandtWarneke](https://avatars.discourse-cdn.com/v4/letter/e/cdc98d/32.png) [@EikeBrandtWarneke](https://forum.shopware.com/u/EikeBrandtWarneke)\
**Post date:** [11. Oktober 2016 um 10:38 UTC](https://forum.shopware.com/t/how-do-i-get-the-second-item-in-an-iteration/40446/2 "2016-10-11T10:38:00Z")

</div>

You are looking for „iteration“:&nbsp;[http://www.smarty.net/docsv2/en/language.function.foreach.tpl](http://www.smarty.net/docsv2/en/language.function.foreach.tpl)

Best regards

---

<div class="post-metadata">

**Author:** ![alex220x](https://avatars.discourse-cdn.com/v4/letter/a/ccd318/32.png) [@alex220x](https://forum.shopware.com/u/alex220x)\
**Post date:** [11. Oktober 2016 um 10:45 UTC](https://forum.shopware.com/t/how-do-i-get-the-second-item-in-an-iteration/40446/3 "2016-10-11T10:45:17Z")

</div>

```
 Thank you for your answer, I have tried the following, but got an error message:

```

&nbsp;

```
{foreach name=$sArticle.sBlockPrices item=$blockPrice name=blockPrices}
  {if $smarty.foreach.blockPrices.index == 2}
    ...
  {/if}
{/foreach}

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![t2oh4e](https://avatars.discourse-cdn.com/v4/letter/t/a5b964/32.png) [@t2oh4e](https://forum.shopware.com/u/t2oh4e)\
**Post date:** [11. Oktober 2016 um 11:12 UTC](https://forum.shopware.com/t/how-do-i-get-the-second-item-in-an-iteration/40446/4 "2016-10-11T11:12:36Z")

</div>

Should be working like that. But that will give you the third enrty since index starts at 0. iteration starts at one. you mixed those two up.

What does the errormessage say?

Perhaps it’s this: you dont need a $ sign in the item parameter. Just item=blockPrice and than it’s available as $blockPrice in the foreach.

---

<div class="post-metadata">

**Author:** ![EikeBrandtWarneke](https://avatars.discourse-cdn.com/v4/letter/e/cdc98d/32.png) [@EikeBrandtWarneke](https://forum.shopware.com/u/EikeBrandtWarneke)\
**Post date:** [11. Oktober 2016 um 11:14 UTC](https://forum.shopware.com/t/how-do-i-get-the-second-item-in-an-iteration/40446/5 "2016-10-11T11:14:13Z")

</div>

Please follow the link:&nbsp;[http://www.smarty.net/docsv2/en/language.function.foreach.tpl](http://www.smarty.net/docsv2/en/language.function.foreach.tpl)  
There are many examples for index and iteration.

Kind regards

---

<div class="post-metadata">

**Author:** ![Stephan\_Pohl](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/stephan_pohl/32/10212_2.png) [@Stephan\_Pohl](https://forum.shopware.com/u/Stephan_Pohl)\
**Post date:** [11. Oktober 2016 um 11:18 UTC](https://forum.shopware.com/t/how-do-i-get-the-second-item-in-an-iteration/40446/6 "2016-10-11T11:18:06Z")

</div>

Hey @alex220x‍,

Smarty 3 provides you with additional information inside the foreach loop, therefore you can use the @index modifier to get the index of the current item:

```
{foreach $sArticle.sBlockPrices as $blockPrice}
  {if $blockPrice@index == 2}
    Second element
  {else}
    ...
  {/if}
{/foreach}

```

Best regards,  
Stephan Pohl&nbsp; ![Shopware](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/86c57fbace799dd4eb1f1b8a2d3bdc1733f0aea1.png "Shopware")
