我有一个有序的树(节点的输出顺序正确)。每个节点都有name,depth和parent_id属性。我试图输出一个嵌套列表,以便我可以直观地看到结构。我在提出正确的方面遇到了一些麻烦。解决这个问题的最佳方法是什么?我是否需要更改数据结构和/或逻辑?
这是数据:
array(24) {
[5]=>
array(3) {
["depth"]=>
int(0)
["name"]=>
string(16) "Basketball Sport"
["parent_id"]=>
NULL
}
[3]=>
array(3) {
["depth"]=>
int(1)
["name"]=>
string(16) "Basketball Shoes"
["parent_id"]=>
string(1) "5"
}
[2]=>
array(3) {
["depth"]=>
int(2)
["name"]=>
string(6) "Jordon"
["parent_id"]=>
string(1) "3"
}
[4]=>
array(3) {
["depth"]=>
int(2)
["name"]=>
string(6) "Lebron"
["parent_id"]=>
string(1) "3"
}
[7]=>
array(3) {
["depth"]=>
int(1)
["name"]=>
string(17) "Basketball Shorts"
["parent_id"]=>
string(1) "5"
}
[8]=>
array(3) {
["depth"]=>
int(2)
["name"]=>
string(22) "Long Basketball Shorts"
["parent_id"]=>
string(1) "7"
}
[9]=>
array(3) {
["depth"]=>
int(2)
["name"]=>
string(23) "Short Basketball Shorts"
["parent_id"]=>
string(1) "7"
}
[10]=>
array(3) {
["depth"]=>
int(0)
["name"]=>
string(10) "Golf Sport"
["parent_id"]=>
NULL
}
[12]=>
array(3) {
["depth"]=>
int(1)
["name"]=>
string(4) "Bags"
["parent_id"]=>
string(2) "10"
}
[13]=>
array(3) {
["depth"]=>
int(2)
["name"]=>
string(9) "Nike Bags"
["parent_id"]=>
string(2) "12"
}
[11]=>
array(3) {
["depth"]=>
int(1)
["name"]=>
string(5) "Clubs"
["parent_id"]=>
string(2) "10"
}
[14]=>
array(3) {
["depth"]=>
int(2)
["name"]=>
string(10) "Nike Clubs"
["parent_id"]=>
string(2) "11"
}
[6]=>
array(3) {
["depth"]=>
int(0)
["name"]=>
string(12) "Tennis Sport"
["parent_id"]=>
NULL
}
[1]=>
array(3) {
["depth"]=>
int(1)
["name"]=>
string(8) "Racquets"
["parent_id"]=>
string(1) "6"
}
[22]=>
array(3) {
["depth"]=>
int(2)
["name"]=>
string(4) "Head"
["parent_id"]=>
string(1) "1"
}
[24]=>
array(3) {
["depth"]=>
int(2)
["name"]=>
string(6) "Prince"
["parent_id"]=>
string(1) "1"
}
[23]=>
array(3) {
["depth"]=>
int(2)
["name"]=>
string(6) "Wilson"
["parent_id"]=>
string(1) "1"
}
[15]=>
array(3) {
["depth"]=>
int(1)
["name"]=>
string(12) "Tennis Shoes"
["parent_id"]=>
string(1) "6"
}
[16]=>
array(3) {
["depth"]=>
int(2)
["name"]=>
string(17) "Mens Tennis Shoes"
["parent_id"]=>
string(2) "15"
}
[19]=>
array(3) {
["depth"]=>
int(3)
["name"]=>
string(23) "Cheap Mens Tennis Shoes"
["parent_id"]=>
string(2) "16"
}
[18]=>
array(3) {
["depth"]=>
int(3)
["name"]=>
string(27) "Expensive Mens Tennis Shoes"
["parent_id"]=>
string(2) "16"
}
[17]=>
array(3) {
["depth"]=>
int(2)
["name"]=>
string(19) "Womens Tennis Shoes"
["parent_id"]=>
string(2) "15"
}
[20]=>
array(3) {
["depth"]=>
int(3)
["name"]=>
string(28) "Women Practical Tennis Shoes"
["parent_id"]=>
string(2) "17"
}
[21]=>
array(3) {
["depth"]=>
int(3)
["name"]=>
string(26) "Women Stylish Tennis Shoes"
["parent_id"]=>
string(2) "17"
}
}
列表输出(注意它如何不断深化):
CODE:
<ul id="category_root">
<?php
$current_depth = 0;
foreach($categories as $category_id => $category_info)
{
//Same level
if ($current_depth == $category_info['depth'])
{
// echo 'SAME LEVEL'. $category_info['name'].'<br />';
echo '<li class="same_level">'.$category_info['name'].'</li>';
}//Sub category
elseif ($current_depth < $category_info['depth'])
{
echo '<ul><li class="subcat_start">'.$category_info['name'];
}//End sub category
elseif($current_depth > $category_info['depth'])
{
echo '</li></ul>'.$category_info['name'];
}
$current_depth = $category_info['depth'];
}
?>
</ul>