Home > Backend Development > PHP Tutorial > The smarty template engine uses the built-in function foreach to loop out all array values, smartyforeach_PHP tutorial

The smarty template engine uses the built-in function foreach to loop out all array values, smartyforeach_PHP tutorial

WBOY
Release: 2016-07-13 10:09:15
Original
853 people have browsed it

The smarty template engine uses the built-in function foreach to loop out all array values, smartyforeach

The example in this article describes how to use smarty’s built-in function foreach, and shares it with everyone for your reference. The details are as follows:

Display file: index.php:

Copy code The code is as follows:
//Create smarty object
require_once("./libs/Smarty.class.php");
$smarty = new Smarty();

$arr1 = array("Beijing","Shanghai","Guangzhou");//Index array
$smarty->assign("arr1",$arr1);//Assign index array
$arr2 = array("city1"=>"Beijing","city2"=>"Shanghai","city3"=>"Guangzhou");//Associative array
$smarty->assign("arr2",$arr2);//Assign associative array
$arr3 = array(array("Beijing","Shanghai","Guangzhou"),array("Guan Yu","Zhang Fei","Beauty"));//Two-dimensional index array
$smarty->assign("arr3",$arr3);
$arr4 = array(array("c1"=>"Beijing","c2"=>"Shanghai","c3"=>"Guangzhou"),array("n1"=>"Guan Yu", "n2"=>"Zhang Fei","n3"=>"Beauty"));//Two-dimensional associative array
$smarty->assign("arr4",$arr4);

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

Template file: temp.tpl

Copy code The code is as follows:

smarty’s built-in function foreach, loops out array values


Example 1: One-dimensional index array


{foreach from=$arr1 item=temp}
{$temp}
{/foreach}

Example 2: One-dimensional associative array——>item is the key value and key is the key name. If the key is not retrieved, the extraction method is the same as the one-dimensional index array. Of course, the index array also has keys 0, 1, 2...


{foreach from=$arr2 item=temp key=k}
{$k}={$temp}
{/foreach}

Example 3: Two-dimensional index array——>Two loops are enough


{foreach from=$arr3 item=temp}
{foreach from=$temp item=value}
{$value}
{/foreach}

{/foreach}

Example 4: Two-dimensional associative array——>Just loop twice


{foreach from=$arr4 item=temp}
{foreach from=$temp item=value key=k}
{$k}={$value}
{/foreach}

{/foreach}

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/946750.htmlTechArticlesmarty template engine uses the built-in function foreach to loop out all array values, smartyforeach This article describes the smarty built-in How to use the function foreach, share with everyone for everyone...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template