The smarty template engine uses the built-in function foreach to retrieve all array values cyclically.

Source: Internet
Author: User
The smarty template engine uses the built-in function foreach to retrieve all array values cyclically. The smarty template engine uses the built-in function foreach to retrieve all array values cyclically. This article describes how to use the smarty built-in function foreach, the smarty template engine uses the built-in foreach function to retrieve all array values cyclically.

This article describes how to use the smarty built-in function foreach and shares it with you for your reference. The details are as follows:

Display File: index. php:

The code is as follows:

<? Php
// Create a smarty object
Require_once ("./libs/Smarty. class. php ");
$ Smarty = new Smarty ();

$ Arr1 = array ("Beijing", "Shanghai", "Guangzhou"); // index array
$ Smarty-> assign ("arr1", $ arr1); // assign an index array
$ Arr2 = array ("city1" => "Beijing", "city2" => "Shanghai", "city3" => "Guangzhou"); // associate an array
$ Smarty-> assign ("arr2", $ arr2); // assign an associated array
$ Arr3 = array ("Beijing", "Shanghai", "Guangzhou"), array ("Guan Yu", "Zhang Fei", "beauty ")); // Two-dimensional index array
$ Smarty-> assign ("arr3", $ arr3 );
$ Arr4 = array ("c1" => "Beijing", "c2" => "Shanghai", "c3" => "Guangzhou "), array ("n1" => "Guan Yu", "n2" => "Zhang Fei", "n3" => "beauty"); // Two-dimensional join array
$ Smarty-> assign ("arr4", $ arr4 );

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

Template File: temp. tpl

The code is as follows:


Smarty's built-in function foreach retrieves array values cyclically

Example 1: one-dimensional index array


{Foreach from = $ arr1 item = temp}
{$ Temp}
{/Foreach}

Instance 2: one-dimensional join array --> item is the key value, and key is the key name. If the key is not obtained, the retrieval method is the same as the one-dimensional index array. of course, the index array also has the key 0, 1, 2...


{Foreach from = $ arr2 item = temp key = k}
{$ K }={ $ temp}
{/Foreach}

Example 3: Two-dimensional index array --> two loops


{Foreach from = $ arr3 item = temp}
{Foreach from = $ temp item = value}
{$ Value}
{/Foreach}

{/Foreach}

Example 4: Two-dimensional join array --> the same two loops.


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

{/Foreach}

I hope this article will help you with php programming.

Examples in this article describes how to use the smarty built-in function foreach...

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.