The example in this article describes the usage of smarty’s built-in function foreach. Share it with everyone for your reference. The details are as follows:
Output file: index.php
$name = array("Xi Yang Yang", "美 Yang Yang", "Lazy Yang", "Ha Ha Ha");
$family = array("husband"=>"Xi Yangyang","wife"=>"Mei Yangyang","boy"=>"Lazy Yang","girl"=>"Hahaha");
$smarty->assign("name",$name);
$smarty->assign("family",$family);
$smarty->display("temp.htm");
?>
Template file: temp.htm
There are roughly two forms of array output methods:
(1) One is smarty 2’s {foreach from=array variable name item=array value}.
(2) The output method of smarty 3 is consistent with that of PHP. The above example does not add $key. When you need to output the key name, use the form @key in the example; you can use the form $key=>$value.
In addition, if you only take a certain value in the array, you can output it directly, such as {$family.husband} for associative arrays, {$name[0]} for index arrays, etc.
I hope this article will be helpful to everyone’s PHP programming design.