考虑一下数组是:
Array
(
[Page-1] => Array
(
[0] => Array
(
[0] => Cat-1
[1] => Item-1
)
)
[Page-2] => Array
(
[0] => Array
(
[0] => Cat-2
[1] => Item-2
)
[1] => Array
(
[0] => Cat-3
[1] => Item-3
)
[2] => Array
(
[0] => Cat-4
[1] => Item-4
)
)
[Page-3] => Array
(
[0] => Array
(
[0] => Cat-5
[1] => Item-5
)
)
[Page-4] => Array
(
[0] => Array
(
[0] => Cat-6
[1] => Item-6
)
)
[Page-5] => Array
(
[0] => Array
(
[0] => Cat-7
[1] => Item-7
)
[1] => Array
(
[0] => Cat-9
[1] => Item-9
)
)
[Page-6] => Array
(
[0] => Array
(
[0] => Cat-8
[1] => Item-8
)
)
)其中,第一个键Page-x数组将是导航菜单中的主链接.
一些主要链接可能有子链接,有些则没有。
子链接是第三个子数组的键的值。
最后,每个链接的URL将是第三个子数组的键1的值。
只有具有多个类别的页面才会将其类别显示为子链接。
我想要的导航栏:
1. <a href="Item-1">Page-1</a>
2. <a href="#">Page-2</a>
<a href="Item-2">Cat-2</a>
<a href="Item-3">Cat-3</a>
<a href="Item-4">Cat-4</a>
3. <a href="Item-5">Page-3</a>
4. <a href="Item-6">Page-4</a>
5. <a href="#">Page-5</a>
<a href="Item-7">Cat-7</a>
<a href="Item-9">Cat-9</a>
6. <a href="Item-8">Page-6</a>PHP代码
$records = $p->main_links();
foreach ($records as $key => $value) {
$return[$value['page']][] = array($value['child'], $value['item']);
}
foreach ($return as $key2 => $value2) {
$count = 0;
/* Select a specific value within the Array */
$main_links = $value2[$count][1]; /* URL of the main Pages */
$count = count($return[$key2]);
if($count > 1) {
foreach ($value2 as $key3 => $value3)
{
$link_name = $value3[0]; /* Child Link Names */
$link_url = $value3[1]; /* URL of Child Links */
/* addedd htmlspecialchars() function to $variables that will be echoed into HTML. It provides some XSS protection */
$cat_link .= '<li><a href="'.htmlspecialchars($filter1.$p->seoUrl($key2).$filter2.$p->seoUrl($link_url)).'">'.htmlspecialchars($link_name).'</a></li>';
}
$result .= '
<li '.htmlspecialchars($li_class).'><a href="#"><span>'.htmlspecialchars($key2).'</span></a>
<ul>
'.$cat_link.'
</ul>
</li>';
}else {
$result .= '
<li><a href="'.htmlspecialchars($filter1.$p->seoUrl($main_links)).'"><span>'.htmlspecialchars($key2).'</span></a></li>';
}
}不幸的是我不能让它起作用..。输出与我所期望的不一样:
当前输出(错误):
1. <a href="Item-1">Page-1</a>
2. <a href="#">Page-2</a>
<a href="Item-2">Cat-2</a>
<a href="Item-3">Cat-3</a>
<a href="Item-4">Cat-4</a>
3. <a href="Item-5">Page-3</a>
4. <a href="Item-6">Page-4</a>
5. <a href="#">Page-5</a>
<a href="Item-2">Cat-2</a>
<a href="Item-3">Cat-3</a>
<a href="Item-4">Cat-4</a>
<a href="Item-7">Cat-7</a>
<a href="Item-9">Cat-9</a>
6. <a href="Item-8">Page-6</a>任何帮助都将不胜感激!
发布于 2014-03-11 02:33:03
我找到了另一条比我想要做的更好的方法。这解决了我的案子。
http://wizardinternetsolutions.com/articles/web-programming/single-query-dynamic-multi-level-menu
谢谢您一直鼓励我!
发布于 2014-03-10 18:06:29
您的当前代码已接近正常工作。这一行将始终产生1的计数。
$count = count($value);我相信,你在那里寻找的是:
$count = count($return[$key]);https://stackoverflow.com/questions/22307605
复制相似问题