There are two tags in thinkphp6 that can realize array looping (volist tag and foreach tag). Let's follow the editor to see the use of these two tags.
First type: volist tag
Syntax:
{volist name="" id="" key="" offset="" length=""} 循环体 {/volist}
Among them:
name: the variable name of the current template;
id: the current loop variable;
key: subscript, starting from 1 by default;
offset: the starting line number;
length: the obtained line number.
Example:
Define array:
<?php namespace app\controller; use think\facade\View; class Test{ public function index(){ $arr=[ ['id'=>1,'name'=>'cmcc' ],['id'=>2,'name'=>'cctv' ],['id'=>1,'name'=>'cmqq' ] ]; view::assign('arr',$arr); return view::fetch(); } } ?>
Traverse in template:
{volist name="arr" id="vv" key="kk" offset="1" length="1"} <div> {$kk} --- {$vv['name']} </div> {/volist}
Output result:
We set the interception to start from 1 and intercept one, so the output result is cctv
.
Second type: foreach tag
Syntax:
{foreach $name as $key=>$id} 循环体 {/foreach}
Among them:
foreach is the same as that of
volist, so we won’t write it here anymore, we will directly Look at the traversal in the template.
{foreach $arr as $k=>$v} <div> {$k} --- {$v['name']} </div> {/foreach}
We can see that the foreach
tag needs to be added when looping. #$ (dollar sign), and the
volist tag does not need to be added with
$ (dollar sign) when recycling.
The latest 10 thinkphp video tutorials
The above is the detailed content of Detailed explanation of two ways of looping in thinkphp6. For more information, please follow other related articles on the PHP Chinese website!