需要帮助来构建一个数组 [英] Help needed to build an array

查看:63
本文介绍了需要帮助来构建一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一张类似
的桌子

ID PARENT


0 | -1

1 | -1

2 | 1

3 | 1

4 | 2

5 | -1

6 | 4


我想构建一个阵列,这样我就可以输出表格中的数据


0

1

| - 2

| ---- 4

| | ---- 5

| - 3

5


其中2是1的孩子,4是2岁的孩子等等。

我可以创建一个像$ data [$ id] [$ parent]这样的数组但是我如何输出

数据ID = 1的所有孩子以及ID =

的所有孩子4.

使用MySQL db和php实现上述目标的最佳方法是什么?


如果我读了PARENT排序的所有数据,我可以一次读取整个

表并将所有数据添加到一个数组中?


有什么想法吗?


问候。

Simon /

Hi

I have a table with something like

ID PARENT

0 | -1
1 | -1
2 | 1
3 | 1
4 | 2
5 | -1
6 | 4

And i want to build an array so i can output the data in the form

0
1
|--2
|----4
| |----5
|--3
5

Where 2 is a child of 1, and 4 is a child of 2 and so on.
I could create an array like $data[$id][$parent] but how would i output the
data where i get all the children of, say, ID=1 and all the children of ID =
4.
What would be the best way to achieve the above using MySQL db and php?

If i read all the data sorted by PARENT i might be able to read the whole
table at once and add all the data to one array?

Any ideas?

Regards.
Simon/

推荐答案

数据


id]


parent]但是如何输出

我得到所有孩子的数据,比如ID = 1,所有孩子的ID =

4.

实现上述目标的最佳方法是什么使用MySQL db和php?


如果我读了所有的数据排序由PARENT编辑我可以一次阅读整个

表并将所有数据添加到一个数组中吗?


任何想法?


问候。

Simon /
parent] but how would i output the
data where i get all the children of, say, ID=1 and all the children of ID =
4.
What would be the best way to achieve the above using MySQL db and php?

If i read all the data sorted by PARENT i might be able to read the whole
table at once and add all the data to one array?

Any ideas?

Regards.
Simon/


这篇关于需要帮助来构建一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆