我有一个表中的以下数据:
+----+---------+-----+---------+--------------+
| id | name | id2 | sname | relation |
+----+---------+-----+---------+--------------+
| 1 | Albaraa | 2 | Brandon | Friend |
| 1 | Albaraa | 3 | Shen | Friend |
| 1 | Albaraa | 4 | Dan | Professor |
| 1 | Albaraa | 5 | Bob | Boss |
| 1 | Albaraa | 6 | Al | God Father |
| 2 | Brandon | 5 | Bob | Friend |
| 3 | Shen | 1 | Albaraa | Friend |
| 3 | Shen | 2 | Brandon | Friend |
| 3 | Shen | 4 | Dan | Professor |
| 4 | Dan | 1 | Albaraa | Student |
| 4 | Dan | 2 | Brandon | Student |
| 4 | Dan | 3 | Shen | Student |
| 5 | Bob | 6 | Al | Best Friend |
| 6 | Al | 1 | Albaraa | God Son |
| 6 | Al | 5 | Bob | Best Friends |
+----+---------+-----+---------+--------------+
我以 php 数组格式获取此数据:
Array (
[0] => Array ( [id] => 1 [name] => Albaraa [id2] => 2 [sname] => Brandon [relation] => Friend )
[1] => Array ( [id] => 1 [name] => Albaraa [id2] => 3 [sname] => Shen [relation] => Friend )
[2] => Array ( [id] => 1 [name] => Albaraa [id2] => 4 [sname] => Dan [relation] => Professor )
[3] => Array ( [id] => 1 [name] => Albaraa [id2] => 5 [sname] => Bob [relation] => Boss )
[4] => Array ( [id] => 1 [name] => Albaraa [id2] => 6 [sname] => Al [relation] => God Father )
[5] => Array ( [id] => 2 [name] => Brandon [id2] => 5 [sname] => Bob [relation] => Friend )
[6] => Array ( [id] => 3 [name] => Shen [id2] => 1 [sname] => Albaraa [relation] => Friend )
[7] => Array ( [id] => 3 [name] => Shen [id2] => 2 [sname] => Brandon [relation] => Friend )
[8] => Array ( [id] => 3 [name] => Shen [id2] => 4 [sname] => Dan [relation] => Professor )
[9] => Array ( [id] => 4 [name] => Dan [id2] => 1 [sname] => Albaraa [relation] => Student )
[10] => Array ( [id] => 4 [name] => Dan [id2] => 2 [sname] => Brandon [relation] => Student )
[11] => Array ( [id] => 4 [name] => Dan [id2] => 3 [sname] => Shen [relation] => Student )
[12] => Array ( [id] => 5 [name] => Bob [id2] => 6 [sname] => Al [relation] => Best Friend )
[13] => Array ( [id] => 6 [name] => Al [id2] => 1 [sname] => Albaraa [relation] => God Son )
[14] => Array ( [id] => 6 [name] => Al [id2] => 5 [sname] => Bob [relation] => Best Friends ) )
我现在需要做的是将数据设置到一个数组中,该数组添加了第一个人,然后从那里添加了他的孩子并添加了孩子的孩子,看起来像这样(让它看起来对你们来说更具可读性 = ]):
Array (
[id] => 0
[name] => Albaraa
[children] => Array (
[0] => Array (
[id] => 1
[name] => Brandon
[data] => Array (
[relationTo] => Albaraa
[relation] => Friend )
[children] => Array (
[id] => 2
[name] => Bob
[data] => Array (
[relationTo] => Brandon
[relation] => Friend )
[children] => Array()
)
...and so on
))
我在它输出上述内容的地方有它,但没有孩子有任何孩子(只是一个空数组)我不确定如何让它正确地做到这一点,我已经尝试过一个类来做到这一点但失败了。 ..因此,任何有关如何使数据看起来像上述数据的帮助将不胜感激!