《PHP實(shí)例:smarty模板引擎使用內(nèi)建函數(shù)foreach循環(huán)取出所有數(shù)組值的方法》要點(diǎn):
本文介紹了PHP實(shí)例:smarty模板引擎使用內(nèi)建函數(shù)foreach循環(huán)取出所有數(shù)組值的方法,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP學(xué)習(xí)本文實(shí)例講述了smarty內(nèi)建函數(shù)foreach的使用辦法,分享給大家供大家參考.具體如下:
PHP學(xué)習(xí)顯示文件:index.php:
PHP學(xué)習(xí)$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);
PHP學(xué)習(xí)$smarty->display("temp.tpl");
?>
PHP學(xué)習(xí)模板文件:temp.tpl
PHP學(xué)習(xí)<p style="color:orange">實(shí)例2:一維關(guān)聯(lián)數(shù)組――>item為鍵值,key為鍵名.如果不取key,取出辦法與一維索引數(shù)組相同,當(dāng)然索引數(shù)組也是有key的0,1,2...</p>
{foreach from=$arr2 item=temp key=k}
{$k}={$temp}
{/foreach}
PHP學(xué)習(xí)<p style="color:red">實(shí)例3:二維索引數(shù)組――>兩次循環(huán)即可</p>
{foreach from=$arr3 item=temp}
?{foreach from=$temp item=value}
? {$value}
?{/foreach}<br />
{/foreach}
PHP學(xué)習(xí)<p style="color:red">實(shí)例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}
PHP學(xué)習(xí)</html>
PHP學(xué)習(xí)希望本文所述對(duì)大家的php程序設(shè)計(jì)有所贊助.
《PHP實(shí)例:smarty模板引擎使用內(nèi)建函數(shù)foreach循環(huán)取出所有數(shù)組值的方法》是否對(duì)您有啟發(fā),歡迎查看更多與《PHP實(shí)例:smarty模板引擎使用內(nèi)建函數(shù)foreach循環(huán)取出所有數(shù)組值的方法》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/12569.html