I have a function that prints out the following into an array like this:
Array ( [0] => Array ( [id] => 1 [name] => name
However I am unable to foreach through this and I am unsure why?
<ul>
{foreach from=$array item=item}
<li>{$item.name}</li>
{/foreach}
</ul>
Update:
{foreach item=topitem from=$getlocations}
{foreach item=item from=$topitem}
<option value="{$item.locationid}">{$item.name}</option>
{/foreach}
{/foreach}
Function:
function getlocations()
{
global $smarty;
$query = placeholderstackoverflow;
return $query;
$smarty->assign('getlocations', $getlocations);
}
Your code actually will run through an associative arrays, assuming you’re using Smarty 3.xx.
Example php code:
Example smarty code:
…generates the following HTML output:
If this isn’t your actual or expected result, please add more code.
Check out,
Update:
Since you’re using Smarty 2.xx, then be advised that foreach has been changed in between.
For walking through a nested array, you cannot just use one foreach, you have to use more of them. Check out example 7.8 in the manual for version 2.xx.