Get first Smarty Array-Element

丶灬走出姿态 提交于 2019-12-05 17:29:42

IF in PHP you have:

$smarty->assign(
    'myVar',
    array('DATA' =>
              array(
                  12 => array('content' => 'first element'),
                  1 => array('content' => 'second element')
              ))
);

In Smarty you can use:

{assign var=first value = $myVar.DATA|@key}
{$myVar.DATA.$first.content}

And you will get displayed:

first element

However if in PHP you use:

$data = array(
    12 => array('content' => 'first element'),
    1 => array('content' => 'second element')
);

next($data);

$smarty->assign(
    'myVar',
    array('DATA' => $data
              )
);

And in Smarty have the same as I showed at the beginning, the result will be:

second element

You would need to call:

reset($data);

after

next($data);

I think you cannot call reset for array in Smarty but I could be wrong.

It's also possible to reset array in Smarty but it's not so easy.

If in PHP you have:

$data = array(
    12 => array('content' => 'first element'),
    1 => array('content' => 'second element')
);

next($data);

$smarty->assign(
    'myVar',
    array('DATA' => $data
              )
);

In Smarty you could use:

{assign var=$myVar.DATA value=$myVar.DATA|@reset}

{assign var=first value = $myVar.DATA|@key}
{$myVar.DATA.$first.content}

And you will get:

first element

as result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!