本文介绍了php显示多层次的treenode菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将此结构显示到多级菜单中?
How do I display this structure into a multi-level menu?
以下是结构:
array
0 =>
array
'product_category_code' => string 'bracelets' (length=9)
'product_category_desc' => string 'Bracelets' (length=9)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:04:08' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-14 22:09:05' (length=19)
1 =>
array
'product_category_code' => string 'floral-dress' (length=12)
'product_category_desc' => string 'Floral Dress' (length=12)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:49' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-30 19:06:58' (length=19)
2 =>
array
'product_category_code' => string 'flowery-bracelets' (length=17)
'product_category_desc' => string 'Flowery Bracelets' (length=17)
'parent_node' => string 'bracelets' (length=9)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:16' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-30 19:08:44' (length=19)
3 =>
array
'product_category_code' => string 'small-flowery-bracelets' (length=23)
'product_category_desc' => string 'Small Flowery Bracelets' (length=23)
'parent_node' => string 'flowery-bracelets' (length=17)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:08:35' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-30 19:09:44' (length=19)
4 =>
array
'product_category_code' => string 'summer-dress' (length=12)
'product_category_desc' => string 'Summer Dress' (length=12)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:29' (length=19)
'modified_by' => string '0' (length=1)
'modified_date' => null
输出应该是这样的:
- 手镯
- 小花手链
这是我所做的,但它仍然显示子节点a
Here is what I did but it still display the child nodes a
function getChildren($rows, $p = 0) { $r = array(); foreach($rows as $row) { if ($row['parent_node']==$p) { var_dump($p); $r[$row['product_category_code']] = getChildren($rows, $row['product_category_code']); } } return $r;
谢谢!
推荐答案
这是因为您已经分配了阵列中的类别。你可以做的是做参数传递参数的函数,并且在foreach循环中有能力从已经分配的类别中清除数组。
It's because you still have the categories in the array when you already assigned them. What you can do is to do the function where you pass the argument as a reference, and the in the foreach loop to have the ability to clear the array from that already assigned category. Simple implementation below.
function getChildren(&$rows, $p = 0) { $r = array(); foreach($rows as $row_id => $row) { if ($row['parent_node']==$p) { $r[$row['product_category_code']] = getChildren($rows, $row['product_category_code']); unset($rows[$row_id]); } } return $r; }
这篇关于php显示多层次的treenode菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!