Home>Article>PHP Framework> Detailed explanation of two ways of looping in thinkphp6
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:
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"}{$kk} --- {$vv['name']}{/volist}
Output result:
We set the interception to start from 1 and intercept one, so the output result iscctv
.
Second type: foreach tag
Syntax:
{foreach $name as $key=>$id} 循环体 {/foreach}
Among them:
foreachis 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}The output result is:{$k} --- {$v['name']}{/foreach}
We can see that theforeach
tag needs to be added when looping. #$(dollar sign), and the
volisttag 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!