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

问题描述 投票:-1回答:2

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

这里是数组的示例。

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循环

php smarty
2个回答
0
投票

尝试一下

   end($array)

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


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} 
© www.soinside.com 2019 - 2024. All rights reserved.