当前位置:Gxlcms > PHP教程 > smarty模板中如何写循环

smarty模板中如何写循环

时间:2021-07-01 10:21:17 帮助过:19人阅读

smarty模板中怎么写循环
研究了半天教程,好像所有的循环都是针对传入数组的

我的问题是这样的:
在一个传入数组中,有8个元素是从i0到i7命名的,现在需要对这个8个元素进行处理
因为这个数组中还包含其它元素,因此不能针对数组进行循环处理
我想的是类似于这样
for ($i=0; $i < 8; $i++)
{
echo $arr["i$i"];
}
我在smarty中嵌入这段php代码,结果好像是获取不过$arr这个变量,导致没有输出。

不知道这个在smarty中该如何实现,请高手指点

------解决方案--------------------
还有个思路你可以在php页面写个数组,里面分别是i0-i7,然后循环这个数组,在里面打印有数据的数组。。
比如你传的数组是$i
PHP code
  1. {foreach from=$i item=item}
  2. {$arr[$item]}<br>
  3. <br><font color="#e78608">------解决方案--------------------</font><br><dl class="code">PHP code<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li>foreach(from=$arr; key=k item=v){</li><li>键:{$k}:值{$v}</li><li>}</li><li><br><font color="#e78608">------解决方案--------------------</font><br>{section name="fooo" loop=$new_re_j}<br>{$new_re_id[fooo]}<br>{/section}<br></li><li><br><font color="#e78608">------解决方案--------------------</font><br>在smarty中嵌套循环吧</li><li><br><font color="#e78608">------解决方案--------------------</font><br>section foreach循环;<br></li><li><br><font color="#e78608">------解决方案--------------------</font><br>{foreach from=$i item=item}<br>{$arr[$item]}<br><br><br><br></li><li><br><font color="#e78608">------解决方案--------------------</font><br>为什么要在smarty中做呢?在php中处理完,然后</li></ol></pre>输出数组显示不是更好吗?
  4. <br><font color="#e78608">------解决方案--------------------</font><br>smarty模板里面一般都是用foreach进行循环<br><br>{{foreach from=$test key=key item=i}}<br>
  5. <br>{{/foreach}} </dl>

人气教程排行