欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

smarty模板引擎使用內(nèi)建函數(shù)foreach循環(huán)取出所有數(shù)組值的方法

 更新時間:2015年01月22日 15:05:52   投稿:shichen2014  
這篇文章主要介紹了smarty模板引擎使用內(nèi)建函數(shù)foreach循環(huán)取出所有數(shù)組值的方法,實例分析了foreach循環(huán)遍歷數(shù)組的幾種常用技巧,需要的朋友可以參考下

本文實例講述了smarty內(nèi)建函數(shù)foreach的使用方法,分享給大家供大家參考。具體如下:

顯示文件:index.php:

復制代碼 代碼如下:
<?php
//創(chuàng)建smarty對象
require_once("./libs/Smarty.class.php");
$smarty = new Smarty();

$arr1 = array("北京","上海","廣州");//索引數(shù)組
$smarty->assign("arr1",$arr1);//分配索引數(shù)組
$arr2 = array("city1"=>"北京","city2"=>"上海","city3"=>"廣州");//關(guān)聯(lián)數(shù)組
$smarty->assign("arr2",$arr2);//分配關(guān)聯(lián)數(shù)組
$arr3 = array(array("北京","上海","廣州"),array("關(guān)羽","張飛","美女"));//二維索引數(shù)組
$smarty->assign("arr3",$arr3);
$arr4 = array(array("c1"=>"北京","c2"=>"上海","c3"=>"廣州"),array("n1"=>"關(guān)羽","n2"=>"張飛","n3"=>"美女"));//二維關(guān)聯(lián)數(shù)組
$smarty->assign("arr4",$arr4);

$smarty->display("temp.tpl");
?>

模板文件:temp.tpl

復制代碼 代碼如下:
<html>
<h2>smarty內(nèi)建函數(shù)foreach,循環(huán)取出數(shù)組值</h2>
<p style="color:green">實例1:一維索引數(shù)組</p>
{foreach from=$arr1 item=temp}
{$temp}
{/foreach}

<p style="color:orange">實例2:一維關(guān)聯(lián)數(shù)組——>item為鍵值,key為鍵名。如果不取key,取出方法與一維索引數(shù)組相同,當然索引數(shù)組也是有key的0,1,2...</p>
{foreach from=$arr2 item=temp key=k}
{$k}={$temp}
{/foreach}

<p style="color:red">實例3:二維索引數(shù)組——>兩次循環(huán)即可</p>
{foreach from=$arr3 item=temp}
 {foreach from=$temp item=value}
  {$value}
 {/foreach}<br />
{/foreach}

<p style="color:red">實例4:二維關(guān)聯(lián)數(shù)組——>同樣兩次循環(huán)即可</p>
{foreach from=$arr4 item=temp}
 {foreach from=$temp item=value key=k}
  {$k}={$value}
 {/foreach}<br />
{/foreach}

</html>

希望本文所述對大家的php程序設(shè)計有所幫助。

相關(guān)文章

最新評論