If you are working with template engines in PHP you probably heard of Smarty.
Official website: http://www.smarty.net

There is a situation that is not well documented on their site, so I thought I will explain it in my blog. As the title says, I am going to talk about listing multidimensional arrays in smarty. Let’s say you want to list a menu, you will probably use a 2 dimensional array, because you need the button’s title, link and maybe other data.

In PHP you will assign the array variable like any regular variable. Example:

$arr[0]['link'] = 'http://shorttag.info';
$arr[0]['title'] = 'Shorttag homepage';

$arr[1]['link'] = 'http://shorttag.info';
$arr[1]['title'] = 'Shorttag contact page';


$smarty->assign("menu", $arr);

In the template the listing of the two dimensional array will look like this:

{foreach from=$menu item=v}
<li><a href="{$v.link}.html">{$v.title}</a></li>

The version above is for the case you don’t need to loop through the second level of the array. Looping through the second level would look like this:

{foreach from=$menu item=v}
{foreach from=$v item=v2}

The second example will display the links and titles in a list. This example is for those of you who use arrays with more levels than 2 (3 dimensional array and so on).