Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store


{foreach} is used for looping over arrays of data. {foreach} has a simpler and cleaner syntax than the {section} loop, and can also loop over associative arrays.

{foreach $arrayvar as $itemvar}

{foreach $arrayvar as $keyvar=>$itemvar}


This foreach syntax does not accept any named attributes. This syntax is new to Smarty 3, however the Smarty 2.x syntax {foreach from=$myarray key="mykey" item="myitem"} is still supported.

  • {foreach} loops can be nested.

  • The array variable, usually an array of values, determines the number of times {foreach} will loop. You can also pass an integer for arbitrary loops.

  • {foreachelse} is executed when there are no values in the array variable.

  • {foreach} properties are @index, @iteration, @first, @last, @show, @total.

  • {foreach} constructs are {break}, {continue}.

  • Instead of specifying the key variable you can access the current key of the loop item by {$item@key} (see examples below).


The $var@property syntax is new to Smarty 3, however when using the Smarty 2 {foreach from=$myarray key="mykey" item="myitem"} style syntax, the $ syntax is still supported.


Although you can retrieve the array key with the syntax {foreach $myArray as $myKey => $myValue}, the key is always available as $myValue@key within the foreach loop.

Option Flags:

Name Description
nocache Disables caching of the {foreach} loop

Example 83. A simple {foreach} loop

= array('red''green''blue');


Template to output $myColors in an un-ordered list

{foreach $myColors as $color}


The above example will output:



Example 84. Demonstrates the an additional key variable

= array('fname' => 'John''lname' => 'Doe''email' => '');


Template to output $myArray as key/value pairs.

{foreach $myPeople as $value}
   <li>{$value@key}: {$value}</li>


The above example will output:

    <li>fname: John</li>
    <li>lname: Doe</li>


Example 85. {foreach} with nested item and key

Assign an array to Smarty, the key contains the key for each looped value.

->assign('contacts', array(
'phone' => '555-555-1234',
'fax' => '555-555-5678',
'cell' => '555-555-0357'),
'phone' => '800-555-4444',
'fax' => '800-555-3333',
'cell' => '800-555-2222')


The template to output $contact.

{* key always available as a property *}
{foreach $contacts as $contact}
  {foreach $contact as $value}
    {$value@key}: {$value}

{* accessing key the PHP syntax alternate *}
{foreach $contacts as $contact}
  {foreach $contact as $key => $value}
    {$key}: {$value}


Either of the above examples will output:

  phone: 555-555-1234
  fax: 555-555-5678
  cell: 555-555-0357
  phone: 800-555-4444
  fax: 800-555-3333
  cell: 800-555-2222


Example 86. Database example with {foreachelse}

A database (PDO) example of looping over search results. This example is looping over a PHP iterator instead of an array().


$smarty = new Smarty

$dsn 'mysql:host=localhost;dbname=test'
$login 'test'
$passwd 'test'

// setting PDO to use buffered queries in mysql is 
  // important if you plan on using multiple result cursors 
  // in the template. 

$db = new PDO($dsn$login$passwd, array( 

$res $db->prepare("select * from users"); 

// assign to smarty 



{foreach $res as $r} 
  .. no results .. 


The above is assuming the results contain the columns named id and name.

What is the advantage of an iterator vs. looping over a plain old array? With an array, all the results are accumulated into memory before being looped. With an iterator, each result is loaded/released within the loop. This saves processing time and memory, especially for very large result sets.

Smarty Template Engine