Rumah > php教程 > php手册 > 标签嵌套超过三层的替代解决方法

标签嵌套超过三层的替代解决方法

WBOY
Lepaskan: 2016-06-07 11:44:20
asal
1369 orang telah melayarinya

虽然不建议构造过于庞大的数组,但实际工作中可能会遇到超过三维的数据需要处理。
手册8.22标签嵌套核心内容如下:默认的嵌套层次是3级,所以嵌套层次不能超过3层,如果需要更多的层次可以指定TAG_NESTED_LEVEL配置参数,例如:<br> 'TAG_NESTED_LEVEL' =>5<br> 可以改变循环嵌套级别为5级。
但是,似乎配置了此参数并未起作用,原因见http://www.thinkphp.cn/topic/3652.html问题中182821的回答。

此前,haran并未实际操作,想当然地照搬手册中的内容,加之基础不牢,对框架源码的理解遭遇瓶颈,解答并未起到解决问题的作用。
-------------------------------------------------------------------------------
但是,经过实践,haran发现,此处的三层只限定为单一标签。所以,haran又祭起笨方法的大旗,变通解决如下(此方法只征对数组,未使用对象测试):
先构建数组:$this->arr = array(<br>     '0'=>array(<br>         '0'=>array(<br>             '0'=>array(<br>                 '0'=>array(<br>                     '0'=>array(<br>                         'id'=>'1',<br>                         'name'=>'haran1'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'2',<br>                         'name'=>'zhang1'<br>                         )<br>                     ),<br>                 '1'=>array(<br>                     '0'=>array(<br>                         'id'=>'21',<br>                         'name'=>'haran2'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'22',<br>                         'name'=>'zhang2'<br>                         )<br>                     )<br>                 ),<br>             '1'=>array(<br>                 '0'=>array(<br>                     '0'=>array(<br>                         'id'=>'31',<br>                         'name'=>'haran3'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'32',<br>                         'name'=>'zhang3'<br>                         )<br>                     ),<br>                 '1'=>array(<br>                     '0'=>array(<br>                         'id'=>'41',<br>                         'name'=>'haran4'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'42',<br>                         'name'=>'zhang4'<br>                         )<br>                     )<br>                 )<br>             ),<br>         '1'=>array(<br>             '0'=>array(<br>                 '0'=>array(<br>                     '0'=>array(<br>                         'id'=>'51',<br>                         'name'=>'haran5'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'52',<br>                         'name'=>'zhang5'<br>                         )<br>                     ),<br>                 '1'=>array(<br>                     '0'=>array(<br>                         'id'=>'61',<br>                         'name'=>'haran6'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'62',<br>                         'name'=>'zhang6'<br>                         )<br>                     )<br>                 ),<br>             '1'=>array(<br>                 '0'=>array(<br>                     '0'=>array(<br>                         'id'=>'71',<br>                         'name'=>'haran7'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'72',<br>                         'name'=>'zhang7'<br>                         )<br>                     ),<br>                 '1'=>array(<br>                     '0'=>array(<br>                         'id'=>'81',<br>                         'name'=>'haran8'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'82',<br>                         'name'=>'zhang8'<br>                         )<br>                     )<br>                 )<br>             )<br>         ),<br>     '1'=>array(<br>         '0'=>array(<br>             '0'=>array(<br>                 '0'=>array(<br>                     '0'=>array(<br>                         'id'=>'91',<br>                         'name'=>'haran9'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'92',<br>                         'name'=>'zhang9'<br>                         )<br>                     ),<br>                 '1'=>array(<br>                     '0'=>array(<br>                         'id'=>'101',<br>                         'name'=>'haran10'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'102',<br>                         'name'=>'zhang10'<br>                         )<br>                     )<br>                 ),<br>             '1'=>array(<br>                 '0'=>array(<br>                     '0'=>array(<br>                         'id'=>'111',<br>                         'name'=>'haran11'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'112',<br>                         'name'=>'zhang11'<br>                         )<br>                     ),<br>                 '1'=>array(<br>                     '0'=>array(<br>                         'id'=>'121',<br>                         'name'=>'haran12'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'122',<br>                         'name'=>'zhang12'<br>                         )<br>                     )<br>                 )<br>             ),<br>         '1'=>array(<br>             '0'=>array(<br>                 '0'=>array(<br>                     '0'=>array(<br>                         'id'=>'131',<br>                         'name'=>'haran13'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'132',<br>                         'name'=>'zhang13'<br>                         )<br>                     ),<br>                 '1'=>array(<br>                     '0'=>array(<br>                         'id'=>'141',<br>                         'name'=>'haran14'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'142',<br>                         'name'=>'zhang14'<br>                         )<br>                     )<br>                 ),<br>             '1'=>array(<br>                 '0'=>array(<br>                     '0'=>array(<br>                         'id'=>'151',<br>                         'name'=>'haran15'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'152',<br>                         'name'=>'zhang15'<br>                         )<br>                     ),<br>                 '1'=>array(<br>                     '0'=>array(<br>                         'id'=>'161',<br>                         'name'=>'haran16'<br>                         ),<br>                     '1'=>array(<br>                         'id'=>'162',<br>                         'name'=>'zhang16'<br>                         )<br>                     )<br>                 )<br>             )<br>         )<br>     );模板代码:<volist><br>     第一层<br><br>     <volist><br>         --第二层<br><br>         <volist><br>             ----第三层<br><br>             <foreach><br>                 ------第四层<br><br>                 <foreach><br>                     --------第五层|||<br><br>                 </foreach><br>             </foreach><br>         </volist><br>     </volist><br> </volist>输出:第一层1<br> --第二层1<br> ----第三层1<br> ------第四层0<br> --------第五层1|haran1||0<br> --------第五层2|zhang1||1<br> ------第四层1<br> --------第五层21|haran2||0<br> --------第五层22|zhang2||1<br> ----第三层2<br> ------第四层0<br> --------第五层31|haran3||0<br> --------第五层32|zhang3||1<br> ------第四层1<br> --------第五层41|haran4||0<br> --------第五层42|zhang4||1<br> --第二层2<br> ----第三层1<br> ------第四层0<br> --------第五层51|haran5||0<br> --------第五层52|zhang5||1<br> ------第四层1<br> --------第五层61|haran6||0<br> --------第五层62|zhang6||1<br> ----第三层2<br> ------第四层0<br> --------第五层71|haran7||0<br> --------第五层72|zhang7||1<br> ------第四层1<br> --------第五层81|haran8||0<br> --------第五层82|zhang8||1<br> 第一层2<br> --第二层1<br> ----第三层1<br> ------第四层0<br> --------第五层91|haran9||0<br> --------第五层92|zhang9||1<br> ------第四层1<br> --------第五层101|haran10||0<br> --------第五层102|zhang10||1<br> ----第三层2<br> ------第四层0<br> --------第五层111|haran11||0<br> --------第五层112|zhang11||1<br> ------第四层1<br> --------第五层121|haran12||0<br> --------第五层122|zhang12||1<br> --第二层2<br> ----第三层1<br> ------第四层0<br> --------第五层131|haran13||0<br> --------第五层132|zhang13||1<br> ------第四层1<br> --------第五层141|haran14||0<br> --------第五层142|zhang14||1<br> ----第三层2<br> ------第四层0<br> --------第五层151|haran15||0<br> --------第五层152|zhang15||1<br> ------第四层1<br> --------第五层161|haran16||0<br> --------第五层162|zhang16||1关键点:volist三层后改用foreach嵌套

AD:真正免费,域名+虚机+企业邮箱=0元

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan