Home > PHP Framework > ThinkPHP > Two methods of loop traversal in ThinkPHP (volist and foreach tags)

Two methods of loop traversal in ThinkPHP (volist and foreach tags)

藏色散人
Release: 2020-01-22 14:12:45
forward
5373 people have browsed it

Two methods of loop traversal in ThinkPHP (volist and foreach tags)

In ThinkPHP, the system provides two tags to facilitate operations: the volist tag and the foreach tag.

volist syntax format:

<volist name=&#39;需要遍历的模板变量名&#39; id=&#39;当前遍历到的元素名&#39;
循环体
</volist>
Copy after login

Note: volist is a double-label statement and needs to be closed.

foreach syntax format:

<foreach name=&#39;需要遍历的模板变量名&#39; item=&#39;当前遍历到的元素名&#39;
循环体
</foreach>
Copy after login

Note:

foreach is also a double-label statement and needs to be closed.

Difference:

The volist syntax format is roughly the same as the foreach syntax format. In addition to the above name and id attribute pairs, volist also supports more attributes. Yes, such as mod, key, length, etc., and the foreach tag only supports key attribute pairs in addition to the above name and item. It can be understood that the foreach tag is a simplified version of the volist tag.

It is recommended to use the volist tag as much as possible in actual operations.

Case 1:

Traversal of one-dimensional array

<?php
...//创建控制器TestController省略
public function test(){
    $array = array(&#39;西游记&#39;,&#39;红楼梦&#39;,&#39;三国演义&#39;,&#39;水浒传&#39;);
    $array2 = array(
    array(&#39;孙悟空&#39;,&#39;猪八戒&#39;,&#39;沙和尚&#39;,&#39;唐僧&#39;),
    array(&#39;贾宝玉&#39;,&#39;薛宝钗&#39;,&#39;刘姥姥&#39;,&#39;林黛玉&#39;),
    array(&#39;刘备&#39;,&#39;关羽&#39;,&#39;张飞&#39;,&#39;曹操&#39;),
    array(&#39;宋江&#39;,&#39;林冲&#39;,&#39;鲁智深&#39;,&#39;时迁&#39;)
    ); 
    $this -> assign(&#39;array&#39;,$array);
   
    $this -> display();
}
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
       <volist name=&#39;array&#39; id=&#39;vol&#39;>
       {$vol} -
       </volist><hr/>
 
       <foreach name=&#39;array&#39; item=&#39;for&#39;>
       {$for} - 
       </foreach>
</body>
</html>
Copy after login

Output display:

Two methods of loop traversal in ThinkPHP (volist and foreach tags)

Case 2:

Traversal of two-dimensional array

<?php
...创建TestController控制器 
public function test(){
   
    $array = array(&#39;西游记&#39;,&#39;红楼梦&#39;,&#39;三国演义&#39;,&#39;水浒传&#39;);
    $array2 = array(
    array(&#39;孙悟空&#39;,&#39;猪八戒&#39;,&#39;沙和尚&#39;,&#39;唐僧&#39;),
    array(&#39;贾宝玉&#39;,&#39;薛宝钗&#39;,&#39;刘姥姥&#39;,&#39;林黛玉&#39;),
    array(&#39;刘备&#39;,&#39;关羽&#39;,&#39;张飞&#39;,&#39;曹操&#39;),
    array(&#39;宋江&#39;,&#39;林冲&#39;,&#39;鲁智深&#39;,&#39;时迁&#39;)
    );
    $this -> assign(&#39;array&#39;,$array);
    $this -> assign(&#39;array2&#39;,$array2);
    $this -> display();
    }
Copy after login
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
/*volist循环遍历,嵌套循环*/
    <volist name=&#39;array2&#39; id=&#39;vol&#39;>
    <volist name=&#39;vol&#39; id=&#39;vo&#39;>
    {$vo}-
    </volist><br/>
    </volist><hr/>
/*foreach循环遍历,嵌套循环*/
<foreach name=&#39;array2&#39; item=&#39;for&#39;>
<foreach name=&#39;for&#39; item=&#39;fo&#39;>
                {$fo}-
</foreach><br/>
</foreach>
</body>
</html>
Copy after login

Output display:

Two methods of loop traversal in ThinkPHP (volist and foreach tags)

More related ThinkPHP For knowledge, please visit ThinkPHP Tutorial!

The above is the detailed content of Two methods of loop traversal in ThinkPHP (volist and foreach tags). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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