PHP/HTML 嵌套导航列表给出了意想不到的结果
我正在用 PHP 编写一个递归导航菜单,以便更轻松地组织和扩展。现在我正在使用《暗黑破坏神 II》中的一些示例来使嵌套正常工作。
递归循环给了我一些奇怪的结果。有时它只返回一个字母而不是数组的名称/url,并且不会递归所有元素。
这有点难以解释,我的主要问题是我不太明白如何调试它。这可能是一些简单的事情,我无法弄清楚。
为了节省大家的时间,我制作了一个页面,其中包含源代码(突出显示语法)并在此处输出:
http:// /radleygh.com/files/test/example.php
这应该可以很容易地看到发生了什么。
列表的布局应如下所示(每个组的名称和 UL 包含在同一 LI 元素中):
ul
li
Parent Title #1
ul
li
Child Title #1
li
Child Title #2
/ul
li
Parent Title #2
/ul
PS:是否有更好的方法来对像我这样的巨大数组进行排序?
谢谢。现在回到调试
I'm writing a recursive navigation menu in PHP for easier organization and expandability. For now I am using some examples from Diablo II to get the nesting to work properly.
The recursive looping is giving me some weird results. Sometimes it only returns a single letter as opposed to the name/url of an array and it doesn't recurse through all elements.
It's a bit difficult to explain, and my main issue is I don't really understand how to debug this. It's probably something simple that I can't figure out.
To save everyone time, I made a page with the source (syntax highlighted) and output here:
http://radleygh.com/files/test/example.php
That should make it easy to see what is going on.
The layout of the list should look like this (With the name and UL of each group contained within the same LI element):
ul
li
Parent Title #1
ul
li
Child Title #1
li
Child Title #2
/ul
li
Parent Title #2
/ul
PS: Is there a better way to go about sorting a huge array like the one I have?
Thanks. Back to debugging for now
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你们非常接近。您的错误所在在
recurse_array
函数中。您正在向其传递单维数组的元素(我知道从技术上讲,给定$element[2]
,它是多维的,但前两个元素是单个元素:)) ,所以当它抓住它时,需要将这些单词分解成它们自己的数组。试试这个:
它应该给你你正在寻找的东西,或者至少是一些更接近你正在寻找的东西。
You were pretty close. Where your mistake lies is within the
recurse_array
function. You are passing it elements of a single dimension array (I know that it is technically multiple dimensions given$element[2]
, but the first two elements are single :)), so when it grabs that, it takes that the words are to be broken out into their own array.Try this:
It should give you what you are looking for, or at least something a bit closer to what you are looking for.