I'm referring from Convert array of paths into UL list
and i found an helpful answer with this function but from what I'm doing is doesn't work properly as I'm having a MySQL Tables structers with id, parentid, name to be able to make recursive category and sub category.
so I'm able to export the data into arrays which is has extra array called CHILDREN has the sub category arrays into it.
but when I try to use this function
function buildUL($array) {
echo "\n<ul>\n";
foreach ($array as $key => $value) {
echo "<li><a href=\"#\">";
echo $value['name'];
if (is_array($value))
$this->buildUL($value['children']);
echo "</a></li>\n";
}
echo "</ul>\n";
}
The problem is that I got multiple output by
<ul>
<li><a href="#">A
<ul>
<li><a href="#">C
<ul>
<li><a href="#">F
<ul>
<li><a href="#">test
<ul>
</ul>
</a></li>
</ul>
</a></li>
</ul>
</a></li>
<li><a href="#">B
<ul>
</ul>
</a></li>
</ul>
</a></li>
<li><a href="#">1
<ul>
<li><a href="#">2
<ul>
</ul>
</a></li>
<li><a href="#">3
<ul>
</ul>
</a>&开发者_JS百科lt;/li>
</ul>
</a></li>
<li><a href="#">99
<ul>
<li><a href="#">Another Test
<ul>
</ul>
</a></li>
<li><a href="#">2 X
<ul>
</ul>
</a></li>
<li><a href="#">Ham Yum
<ul>
</ul>
</a></li>
<li><a href="#">Be You
<ul>
</ul>
</a></li>
<li><a href="#">1 Z
<ul>
</ul>
</a></li>
</ul>
</a></li>
</ul>
I believe you should check if is set value['children'] because as i see one line above
echo $value['name'];
$value should always be an array so you are getting into recursion on every element
try using
if ( isset($value['children'] && is_array($value['children']) )
Instead of just if (is_array($value))
function buildUL($array) {
echo "\n<ul>\n";
foreach ($array as $key => $value) {
echo "<li><a href=\"#\">";
echo $value['name'];
echo "</a></li>\n";
if (!empty($value['children']))
$this->buildUL($value['children']);
}
echo "</ul>\n";
}
try this one...
By the way it is not proper to have <a ...> text <ul><li><a ...> test2 </a></li></ul> </a>
精彩评论