如何将平面数组排序为多维树
我有一个表格,例如
id catagory suboff
1 software 0
2 programming 1
3 Testing 1
4 Designing 1
5 Hospital 0
6 Doctor 5
7 Nurses 5
9 Teaching 0
10 php programming 2
11 .net programming 2
如何编写代码以根据 suboff 获取多维数组中的所有这些信息,如下所示,
-software
--programming
---php programming
--- .net programming
--testing
--designing
-hospital
--doctor
--nurses
-teaching
I have a table like
id catagory suboff
1 software 0
2 programming 1
3 Testing 1
4 Designing 1
5 Hospital 0
6 Doctor 5
7 Nurses 5
9 Teaching 0
10 php programming 2
11 .net programming 2
How to write a code to get all these information in a multidimensional array based on the suboff as follows,
-software
--programming
---php programming
--- .net programming
--testing
--designing
-hospital
--doctor
--nurses
-teaching
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
假设 MySQL 作为您的数据库引擎:
使用 引用 来完成这样的工作。
Assuming MySQL as your DB engine:
Use references for a job like this.
演示:http://ideone.com/vk4po
结果...
Demo: http://ideone.com/vk4po
Result...
我的做法是:
首先您需要解析该表。我想你可以自己做;如果没有,谷歌“正则表达式”,它们是你的朋友。
您正在使用的数据结构是经典的树。您将需要两个数组来使用它。首先是一个节点数组,
$nodes
,其中键是节点 ID,值是节点名称,以及$links
,其中每个键是父节点,每个value 是一个子数组($links[$id][] = $suboff
对于每个元素就足够了)。现在你必须递归地下降你拥有的树。您引入一个具有如下签名的函数:
此函数应该使用 $level 填充破折号打印节点本身(存储在 $nodes 中的信息),并调用自身来渲染所有子节点。它们将依次渲染所有子节点等。您只需为顶级节点调用此函数即可。
The way I would do that:
First you need to parse this table. I assume you can do it yourself; if not, Google "regular expressions", they are your friends.
The data structure you are working with is a classical tree. You will need two arrays to work with it. First is an array of nodes,
$nodes
, where the keys are the node IDs and values are node names, and$links
, where each key is a parent node and each value is an array of children ($links[$id][] = $suboff
for each element would suffice).Now you have to recursively descent the tree you have. You introduce a function with a signature like this:
This function should print the node itself (info stored in $nodes) with $level padding dashes and call itself to render all children nodes. They will in turn render all their subnodes, etc. You just have to call this function for top-level nodes.
此类将平面类别数组转换为结构化树数组:
This class converst a flat category array into a structured tree array:
这是我刚刚为我的应用程序编写的内容,它就像一个魅力:)
这是结果:
This is what I just wrote for my app, and it works like a charm :)
And here is the result:
恕我直言,逻辑是:
IMHO the logic is:
您需要将整个表读入内存并将其转换为一棵树,其中每个节点都可以用其相应的 ID 号进行标识。然后对树进行预序遍历并将其打印出来。
You'll want to read the whole table into memory and turn it into a tree where each node can be identified with its corresponding id number. Then do a pre-order traversal of the tree to print it out.
在 PHP wenn 中,我从数据库获取数据:
In PHP wenn i get the data from a Database:
这是一种不同的方法,应该很容易理解。它要求您将表按
suboff
排序,即假设结果存储在
$table
中,您可以使用这个非常简洁的 php 代码:Here is a different approach that should be very easy to understand. It requires you to have the table ordered by
suboff
, i.e.Assuming the result is stored in
$table
, you can use this very concise php code:这个解决方案对我来说效果很好。
This solution works fine for me.