I am trying to access a nested array inside html code using smarty. For example i have an array that looks like this: Array(customer)[name, age, id[firstname, lastname, birthdate]]. I have been trying something like this:
{foreach from=$customer item=foo}
Customer id: {$foo.id}
{/foreach}
But this doesent print any values to the page. However with this syntax
{foreach from $customer item=foo}
{$foo.id} // or {$foo.id.0}
{/foreach}
It prints out: Array
How do i access the values of nested array using smarty?
Try this:
{foreach from=$customer item=foo}
Customer name: {$foo.id.firstname} {$foo.id.lastname}
{/foreach}
Smarty
will compile it to something equivalent with:
foreach ($customer as $foo) {
echo('Customer name: '.$foo['id']['firstname'].' '.$foo['id']['laststname']);
}
Your $foo.id is an actual array, so you should iterate over it as well.
{foreach from=$customer item=foo}
Contained values: {$foo.id.firstname} {$foo.id.lastname} {$foo.id.birthdate}
{/foreach}