简体   繁体   English

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

[英]How to get the last element from an array in Smarty

I'm trying to retreive the last array from an smarty array.我正在尝试从 smarty 数组中检索最后一个数组。

Here is a sample of the array.这是数组的示例。

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

I have tried the following method How to get last element of an array in smarty templates but it doesn't work for my case.我尝试了以下方法如何在 smarty 模板中获取数组的最后一个元素,但它不适用于我的情况。

I tried {$array|@end} and {$array.id|@end}我试过{$array|@end}{$array.id|@end}

How I retreive normally {$array[1].id|@end} but this is not stable.我如何正常检索{$array[1].id|@end}但这并不稳定。 I would like to avoid using foreach loop我想避免使用foreach循环

you can give this a try if it help you如果它对你有帮助,你可以试试这个

{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} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM