Home  >  Article  >  Backend Development  >  smarty 遍历多维数组问题

smarty 遍历多维数组问题

WBOY
WBOYOriginal
2016-06-23 14:24:411086browse

本帖最后由 swordnorth 于 2013-10-21 10:10:51 编辑

smarty

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版的

	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);

不考虑用:smarty 实现递归了
用php递归实现

smarty只实现了一些基础的功能,高级功能需要写插件

smarty只实现了一些基础的功能,高级功能需要写插件


已经结贴给分了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn