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

jerry thinkphp 2015年11月18日 收藏
虽然不建议构造过于庞大的数组,但实际工作中可能会遇到超过三维的数据需要处理。
手册8.22标签嵌套核心内容如下:
默认的嵌套层次是3级,所以嵌套层次不能超过3层,如果需要更多的层次可以指定TAG_NESTED_LEVEL配置参数,例如:
'TAG_NESTED_LEVEL' =>5
可以改变循环嵌套级别为5级。

但是,似乎配置了此参数并未起作用,原因见http://www.thinkphp.cn/topic/3652.html问题中182821的回答。

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