首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

smarty 遍历多维数组有关问题

2013-10-22 
smarty 遍历多维数组问题本帖最后由 swordnorth 于 2013-10-21 10:10:51 编辑Array([0] Array([0] 1

smarty 遍历多维数组问题
本帖最后由 swordnorth 于 2013-10-21 10:10:51 编辑


Array
(
    [0] => Array
        (
            [0] => 1
            [id] => 1
            [1] => 设置
            [name] => 设置
            [2] => 0
            [parentid] => 0
            [child] => Array
                (
                    [0] => Array
                        (
                            [0] => 20
                            [id] => 20
                            [1] => 管理员管理
                            [name] => 管理员管理
                            [2] => 1
                            [parentid] => 1
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => 23
                                            [id] => 23
                                            [1] => 角色管理
                                            [name] => 角色管理
                                            [2] => 20
                                            [parentid] => 20
                                        )

                                    [1] => Array
                                        (
                                            [0] => 22


                                            [id] => 22
                                            [1] => 管理员管理
                                            [name] => 管理员管理
                                            [2] => 20
                                            [parentid] => 20
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [0] => 19
                            [id] => 19
                            [1] => 相关设置
                            [name] => 相关设置
                            [2] => 1
                            [parentid] => 1
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => 25
                                            [id] => 25
                                            [1] => 模型管理
                                            [name] => 模型管理
                                            [2] => 19
                                            [parentid] => 19


                                        )

                                    [1] => Array
                                        (
                                            [0] => 24
                                            [id] => 24
                                            [1] => 菜单管理
                                            [name] => 菜单管理
                                            [2] => 19
                                            [parentid] => 19
                                        )

                                    [2] => Array
                                        (
                                            [0] => 21
                                            [id] => 21
                                            [1] => 基本设置
                                            [name] => 基本设置
                                            [2] => 19
                                            [parentid] => 19
                                        )

                                )

                        )

                )

        )




<{foreach from=$arr item=m}>
            ID: <{$m.id}>  <{$m.name}> <{$m.id}><br>
            <{foreach from=$m.child item=item}>


                  <{$item.name}> <{$item.id}><br>
                    <{foreach from=$item.child item=items}>
                            <{$items.name}> <{$items.id}><br>
                    <{/foreach}>
            <{/foreach}>
        <{/foreach}>



如何遍历多维数组呢? 如果再有下级,smarty 应该如何遍历无限多? smarty
[解决办法]
你需要递归遍历数组,以下面的例子为参考,自己改个smarty版的

function bianli($array){
foreach($array as $value){
if(is_array($value)){
bianli($value);
}else{
echo $value."\n";
}
}
}
$a=array('A','B',array('c','d',array('e','f')),'g');
echo bianli($a);

热点排行