search
HomePHP FrameworkThinkPHPTwo methods of loop traversal in ThinkPHP (volist and foreach tags)

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>

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

foreach syntax format:

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

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>

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();
    }
<!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>

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!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
What Are the Key Features of ThinkPHP's Built-in Testing Framework?What Are the Key Features of ThinkPHP's Built-in Testing Framework?Mar 18, 2025 pm 05:01 PM

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?How to Use ThinkPHP for Building Real-Time Stock Market Data Feeds?Mar 18, 2025 pm 04:57 PM

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?What Are the Key Considerations for Using ThinkPHP in a Serverless Architecture?Mar 18, 2025 pm 04:54 PM

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?How to Implement Service Discovery and Load Balancing in ThinkPHP Microservices?Mar 18, 2025 pm 04:51 PM

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

What Are the Advanced Features of ThinkPHP's Dependency Injection Container?What Are the Advanced Features of ThinkPHP's Dependency Injection Container?Mar 18, 2025 pm 04:50 PM

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

How to Use ThinkPHP for Building Real-Time Collaboration Tools?How to Use ThinkPHP for Building Real-Time Collaboration Tools?Mar 18, 2025 pm 04:49 PM

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?What Are the Key Benefits of Using ThinkPHP for Building SaaS Applications?Mar 18, 2025 pm 04:46 PM

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?How to Build a Distributed Task Queue System with ThinkPHP and RabbitMQ?Mar 18, 2025 pm 04:45 PM

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools