我正在尝试对 Parent -child 和他们的孩子进行分类,等等我有一个数组,其中包含子项意味着子项。
$navarray= Array
(
[0] => Array
(
[id] => 3
[name] => add
[controller] => ClassYearsSubjectsController
[action] => getsubjects
[parent] => 0
[sub] => Array
(
)
)
[1] => Array
(
[id] => 1
[name] => occupations
[controller] => OccupationsController
[action] => index
[parent] => 0
[sub] => Array
(
[0] => Array
(
[id] => 7
[name] => test
[controller] => AddressesController
[action] => index
[parent] => 1
[sub] => Array
(
[0] => Array
(
[id] => 8
[name] => testnew
[controller] => AdmissionEnquiriesController
[action] => giveadmissions
[parent] => 7
[sub] => Array
(
)
)
)
)
[1] => Array
(
[id] => 2
[name] => add
[controller] => OccupationsController
[action] => add
[parent] => 1
[sub] => Array
(
)
)
)
)
)
我正在尝试生成树。 但我试过了
<?php echo GenerateNavHTML($navarray); ?>
和功能GenerateNavHTML
<?php
function GenerateNavHTML($nav)
{
$html = '';
foreach($nav as $page)
{
$html.='<ul><li>';
$html .= '<a href="#">k</a>';
$html .= GenerateNavHTML($page['sub']);
$html .= '</li></ul>';
}
return $html;
}
?>
我的输出是
<ul style="display: block;" class="nav">
<li class="test"><a href="">add</a>
</li>
</ul><ul style="display: block;" class="nav"><li class="test"><a href="">occupations</a><ul style="display: block;" class="nav"><li class="test"><a href="">test</a><ul style="display: block;" class="nav"><li class="test"><a href="">testnew</a></li></ul></li></ul><ul style="display: block;" class="nav"><li class="test"><a href="">add</a></li></ul></li></ul>
但我期待这个输出。
<ul style="display: block;" class="nav">
<li class="test">
<li class="test">
<a class="parent" href="#">Shoes</a>
<ul>
<li>
<a class="parent" href="#">Womens</a>
<ul>
<li><a href="#">Sandals</a></li>
<li><a href="#">Sneakers</a></li>
<li><a href="#">Wedges</a></li>
<li><a href="#">Heels</a></li>
<li><a href="#">Loafers</a></li>
<li><a href="#" class="last" >Flats</a></li>
</ul>
</li>
<li>
<a class="parent last" href="#">Mens</a>
<ul>
<li><a href="#">Loafers</a></li>
<li><a href="#">Sneakers</a></li>
<li><a href="#">Formal</a></li>
</ul>
</li>
</ul>
</li>
</ul>
Can anybody tell what logic i need to change
Thanks