如何从Smarty中的数组获取最后一个元素

时间:2020-03-20 12:21:22

标签: php smarty

我正试图从一个聪明的数组中获取最后一个数组。

这是数组的示例。

Array (2)
0 => Array (3)
  id => "1"
  name => "John"
  age => "25"
1 => Array (3)
  id => "2"
  name => "Doe"
  age => "16"

我尝试了以下方法How to get last element of an array in smarty templates,但不适用于我的情况。

我尝试了{$array|@end}{$array.id|@end}

我如何正常撤退{$array[1].id|@end},但这并不稳定。我想避免使用foreach循环

2 个答案:

答案 0 :(得分:0)

尝试一下

   end($array)

参考https://www.php.net/manual/en/function.end.php

答案 1 :(得分:0)

如果有帮助,您可以尝试一下

{foreach from=$array key=myId item=i name=foo}
  {if $smarty.foreach.foo.last}
      <p>This is the last item from the array!</p>
  {/if}
{/foreach} 
相关问题