我正在为网站构建导航。我的所有数据都存储在在线 JSON 上,这是一个示例:
{
"State": 0,
"Tasks": [
{
"Id": 1000000,
"ParentId": 0,
"TaskCode": "1 ",
"Name": "item 1",
"Description": "item 1",
"Url": "",
"Parameter": ""
},
{
"Id": 1010000,
"ParentId": 1000000,
"TaskCode": "1.1 ",
"Name": "item 1.1",
"Description": "item 1.1",
"Url": "",
"Parameter": ""
},
{
"Id": 1010100,
"ParentId": 1010000,
"TaskCode": "1.1.1 ",
"Name": "item 1.1.1",
"Description": "item 1.1.1",
"Url": "",
"Parameter": ""
},
{
"Id": 1010200,
"ParentId": 1010000,
"TaskCode": "1.1.2 ",
"Name": "item 1.1.2",
"Description": "item 1.1.2",
"Url": "",
"Parameter": ""
},
{
"Id": 1010300,
"ParentId": 1010000,
"TaskCode": "1.1.3 ",
"Name": "item 1.1.3",
"Description": "item 1.1.3",
"Url": "",
"Parameter": ""
},
{
"Id": 1010400,
"ParentId": 1010000,
"TaskCode": "1.1.4 ",
"Name": "item 1.1.4",
"Description": "item 1.1.4",
"Url": "",
"Parameter": ""
},
{
"Id": 1020000,
"ParentId": 1000000,
"TaskCode": "1.2 ",
"Name": "item 1.2",
"Description": "item 1.2",
"Url": "",
"Parameter": ""
},
{
"Id": 1030000,
"ParentId": 1000000,
"TaskCode": "1.3 ",
"Name": "item 1.3",
"Description": "item 1.3",
"Url": "",
"Parameter": ""
}
]
}
然后我创建一个这样的函数:
function buildNavigation($items, $parent = '0')
{
$hasChildren = false;
$outputHtml = '<ul>%s</ul>';
$childrenHtml = '';
foreach($items as $item)
{
$curr_parent = $item->Id;
if ($item->ParentId == $parent) {
$hasChildren = true;
$childrenHtml .= "<li>";
$childrenHtml .= "<a href='" . $item->Url . "'><img alt='' src='xxx.png'>";
$childrenHtml .= trim($item->TaskCode).". " . $item->Name . "</a>";
foreach($items as $child)
{
$cur_node = $child->Id;
if ($child->ParentId == $curr_parent) {
$hasChildren = true;
$childrenHtml .= '<ul>';
$childrenHtml .= '<li>';
$childrenHtml .= '<a href="' . $child->Url . '">';
$childrenHtml .= trim($child->TaskCode) . ". " . $child->Name . "</a>";
foreach($items as $child2)
{
$cur_node2 = $child2->Id;
if ($child2->ParentId == $cur_node) {
$hasChildren = true;
$childrenHtml .= '<ul>';
$childrenHtml .= '<li>';
$childrenHtml .= '<a href="' . $child2->Url . '">';
$childrenHtml .= trim($child2->TaskCode).". " . $child2->Name . "</a>";
$childrenHtml .= '</li>';
$childrenHtml .= '</ul>';
}
}
$childrenHtml .= '</li>';
$childrenHtml .= '</ul>';
}
}
$childrenHtml .= '</li>';
}
}
// Without children, we do not need the <ul> tag.
if (!$hasChildren) {
$outputHtml = '';
}
// Returns the HTML
return sprintf($outputHtml, $childrenHtml);
}
echo buildNavigation($json_data->Tasks);
但我在 html 上得到的是这样的:
<ul>
<li>
<a href=''><img alt='' src='xxx.png'>1. item 1</a>
<ul>
<li>
<a href="">1.1. item 1.1</a>
<ul>
<li><a href="">1.1.1. item 1.1.1</a></li>
</ul>
<ul>
<li><a href="">1.1.2. item1.1.2</a></li>
</ul>
<ul>
<li><a href="">1.1.3. item1.1.3</a></li>
</ul>
<ul>
<li><a href="">1.1.4. item1.1.4</a></li>
</ul>
<!-- .... (and more) -->
应该是这样的:
<ul>
<li>
<a href=''><img alt='' src='xxx.png'>1. item1</a>
<ul>
<li>
<a href="">1.1. item1.1</a>
<ul>
<li><a href="">1.1.1. item1.1.1</a></li>
<li><a href="">1.1.2. item1.1.2</a></li>
<li><a href="">1.1.3. item1.1.3</a></li>
<li><a href="">1.1.4. item1.1.4</a></li>
</ul>
</li>
</ul>
</li>
</ul>
任何人都可以帮助改进我的 PHP 功能吗?
谢谢。