The Smarty template engine uses the built-in function foreach loop to fetch all the array values _php instance

Source: Internet
Author: User
Tags smarty template

This article describes the use of Smarty built-in function foreach, shared for everyone to reference. Specifically as follows:

Display file: index.php:

Copy Code code as follows:
<?php
Creating Smarty Objects
Require_once ("./libs/smarty.class.php");
$smarty = new Smarty ();

$arr 1 = Array ("Beijing", "Shanghai", "Guangzhou");//index array
$smarty->assign ("arr1", $arr 1);//Allocate index array
$arr 2 = Array ("City1" => "Beijing", "City2" => "Shanghai", "City3" => "Guangzhou");//associative array
$smarty->assign ("arr2", $arr 2);//Allocate associative array
$arr 3 = Array ("Beijing", "Shanghai", "Guangzhou"), Array ("Guan Yu", "Zhang Fei", "Beauty"));//two-dimensional indexed array
$smarty->assign ("Arr3", $arr 3);
$arr 4 = Array ("C1" => "Beijing", "C2" => "Shanghai", "C3" => "Guangzhou"), Array ("N1" => "Guan Yu", "N2" => "Zhang Fei", "N3" => "Beauty")) ;//two-dimensional associative array
$smarty->assign ("Arr4", $arr 4);

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

Template file: Temp.tpl

Copy Code code as follows:
<p style= "Color:green" > Instance 1: one-dimensional indexed array </p>
{foreach from= $arr 1 item=temp}
{$temp}
{/foreach}

<p style= "Color:orange" > Instance 2: One-dimensional associative array-->item is the key value and key is the key name. If you do not take the key, the extraction method is the same as the one-dimensional index array, and the index array is also the key 0,1,2...</p>
{foreach from= $arr 2 item=temp key=k}
{$k}={$temp}
{/foreach}

<p style= "Color:red" > Example 3: Two-dimensional indexed array--> two cycles can </p>
{foreach from= $arr 3 item=temp}
{foreach from= $temp item=value}
{$value}
{/foreach}<br/>
{/foreach}

<p style= "color:red" > Example 4: Two-dimensional associative array--> the same two cycles can be </p>
{foreach from= $arr 4 item=temp}
{foreach from= $temp item=value key=k}
{$k}={$value}
{/foreach}<br/>
{/foreach}

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.