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...