Wednesday, September 26, 2012

Access to the associative array index in Smarty


<?php
$users = array();
$users[1] = array('fname' => 'John', 'lname' => 'Smith');
$users[2] = array('fname' => 'James', 'lname' => 'Bond');

$ids = array();
$ids[] = array('uid' => 1);
$ids[] = array('uid' => 2);

$smarty->assign('users', $users);
$smarty->assign('ids', $ids);
?>

{foreach item=user from=$ids}
    {$users[$user.uid].fname} {$users[$user.uid].lname}
{/foreach}

or

{foreach item=user from=$ids}
    {assign var=u_id value=$user.uid}
    {$users.$u_id.fname} {$users.$u_id.lname}
{/foreach}


No comments:

Post a Comment