循环地在 PHP 数组中创建层次结构
我已经和这样的问题斗争了几个小时了。为了加快我的网页速度,我请求数据库一次获取所有类别,然后想使用 PHP 对数组进行排序。
Array
(
[0] => Array
(
[id] => 1
[name] => name1
[parent] => 0
[children] =>
)
[1] => Array
(
[id] => 2
[name] => name2
[parent] => 1
[children] =>
)
)
我需要得到这样的东西,
Array
(
[0] => Array
(
[id] => 1
[name] => name1
[parent] => 0
[children] => Array
(
[id] => 2
[name] => name2
[parent] => 1
[children] =>
)
)
)
问题是使它适用于任何层次结构。这样就可以循环工作了。请帮忙!
I've been fighting for hours with such a problem. For speeding my web-page I request database to get all categories just one time and then want to sort the array using PHP.
Array
(
[0] => Array
(
[id] => 1
[name] => name1
[parent] => 0
[children] =>
)
[1] => Array
(
[id] => 2
[name] => name2
[parent] => 1
[children] =>
)
)
I need to get something like this
Array
(
[0] => Array
(
[id] => 1
[name] => name1
[parent] => 0
[children] => Array
(
[id] => 2
[name] => name2
[parent] => 1
[children] =>
)
)
)
The problem with that is making it for any level of hierarchy. So that it could work in cycle. Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有很多解决方案可以减少这里的开销。但如果不知道您的限制是什么,就很难推荐一种方法。
例如:
使用邻接模型 - 请参阅我对 dnagirl 的答案的评论
将所有数据加载到 PHP 中,然后使用递归算法创建嵌套树(这会相当慢,并且会受益于一些缓存)
write返回按深度优先树遍历排序的结果集的递归存储过程
以示例 2 为例,更接近代码... .像...之类的东西
There are lots of solutions to reducing the overhead here. But without knowing what your constraints are, it's difficult to recommend an approach.
e.g.:
use an adjacency model - see my comment on dnagirl's answer
load all the data into PHP then use a recursion algorithm to create the nested tree (this will be rather slow and would benefit from some caching)
write a recursive stored procedure which returns the result set sorted by a depth-first tree walk
Taking example 2 a bit closer to code....something like....
您正在为层次结构使用邻接模型。您必须多次查询数据库,或者如果级别数已知,则为每个级别构造一个带有子句的联合查询。
如果允许,您可能会发现单击父级时调用子级的 AJAX 解决方案既更容易编程,也更高效。
You're using an adjacency model for your hierarchy. You will either have to query your db multiple times, or if the number of levels is known, construct a union query with a clause for each level.
If it is allowed, you'll likely find that an AJAX solution where child levels are called when the parent is clicked is both easier to program and more efficient.
这期望父母总是先于孩子被填充
This expects parents to always be populated before its children
你正在寻找的是所谓的递归,对我来说,它不是很清楚如何在 php 中处理递归,并且关于这个主题的文档几乎是空白的。
您可能有兴趣查看此和这篇文章
What you are looking for is called recursion, as for me it is not very clear how to handle recursions in php and documentation is almost blank regarding this subject.
You might be interested to check this and this article
不确定您是否找到了答案,但我今天一直在寻找相同的解决方案,最后最终制定了自己的解决方案。下面的代码是我刚刚创建的类,它适用于 PHP 数组和对象,并且递归到无限数量的维度。
GitHub
https://github.com/DukeOfMarshall/PHP-Array -Heirarchy-Display使用此代码的一个简单示例是:
还可以设置其他选项,但这只是数组或对象的直接层次结构显示。
Not sure if you've found an answer yet, but I was looking for the same solution today and finally ended up making my own solution. The below code is the class I just created and it works with PHP arrays and objects and is recursive to an unlimited number of dimensions.
GitHub
https://github.com/DukeOfMarshall/PHP-Array-Heirarchy-DisplayA simple example of using this code would be:
There are other options that can be set as well, but that was just a straight up heirarchal display of an array or object.