0 =>
array (size=12)
'id' => string '1' (length=1)
'name' => string 'Test' (length=13)
'parent_id' => string '0' (length=1)
'path' => string '0,' (length=2)
'order' => string '100' (length=3)
'from' => string '0' (length=1)
'qyid' => null
'lng' => string '103.86182' (length=9)
'lat' => string '36.053103' (length=9)
'level' => null
'desc' => null
'level_code' => null
1 =>
array (size=12)
'id' => string '46' (length=2)
'name' => string 'Test2' (length=9)
'parent_id' => string '1' (length=1)
'path' => string '0,1' (length=3)
'order' => string '0' (length=1)
'from' => string '0' (length=1)
'qyid' => null
'lng' => null
'lat' => null
'level' => null
'desc' => null
'level_code' => null
我有如上的一个数组,其中parent_id代表该节点的父节点。我现在打算生成一棵树。我接到前端的html如下:

Test
- Test2
html无限级分类前端输出输出,无限级分类问题 - Test3
html无限级分类前端输出输出,无限级分类问题 - Test4
html无限级分类前端输出输出,无限级分类问题
-
大家也可以忽略上面的html。
那么递归该怎么写呢??? 虚心求教~~
回答:
首先,你需要一个树的数据结构来按照一定的结构来组织你的数据,这个树当中的每个节点用如下的TreeNode来表示,然后递归的遍历你的数据,在遍历的过程中动态的生成这个由TreeNode组成的树。并返回到前台。在前台再遍历一下,就能组成你要的效果了
public class TreeNode {
'id' => string '1' (length=1)
'name' => string 'Test' (length=13)
'parent_id' => string '0' (length=1)
'path' => string '0,' (length=2)
'order' => string '100' (length=3)
'from' => string '0' (length=1)
'qyid' => null
'lng' => string '103.86182' (length=9)
'lat' => string '36.053103' (length=9)
'level' => null
'desc' => null
'level_code' => null
public List nodes;
}
回答:
无限级json的功能估计是菜单了。
和楼上说的差不多,建个多叉树。你想要的结果就是多叉树前序遍历之后的结果。python的我实现过~~php的只能提供解决方案了~(如果兄弟之间要求有序的话还需要加其他的操作)找到一个php的
希望有帮助。
回答:
我也正在做这个功能,终于弄出来了
我的数据库结构
实现的效果
贴代码
def _load_menuTree(self):
'加载系统菜单'
menus = api.resource.Resource().all(fields='id, pid, menu_name, resource_uri', display = 'S')
_items = {r.id:r for r in menus}
_items = defaultdict(hash, _items)
for k, r in _items.items():
r['children'] = deque()
retval = deque()
for k, r in _items.items():
if r.pid in _items:
_items[r.pid]['children'].append(_items[r.id])
else:
retval.append(_items[r.id])
return retval