制作一个数组,生成记录的父项和子项
我无法弄清楚如何将父级、子级(通过 id)从数组(由数据库加载)放入新数组中。
我需要它是这样的:
+- Parent - ID: 4
|
+---- Child record
+---- Child record <-- these children have a parent_id of 4
+---- Child record
|
+- Parent - ID: 5
|
+---- Child record
+---- Child record
+---- Child record
+---- Child record <-- these children have a parent_id of 5
+---- Child record
|
+- Parent - ID: 7
|
+---- Child record
+---- Child record <-- these children have a parent_id of 7
+---- Child record
依此类推,从数据库加载的记录如下所示:
Array
(
[0] => Array
(
[id] => 1
[info] => this is a child, since sub is 1 and parent_id contains a number
[sub] => 1
[parent_id] => 4
)
[1] => Array
(
[id] => 2
[info] => this is a parent, since sub is 0 and parent_id does not contain a number
[sub] => 0
[parent_id] =>
)
[2] => Array
(
[id] => 1
[info] => this is a child, since sub is 1 and parent_id contains a number
[sub] => 1
[parent_id] => 4
)
.... more records
SQL 按行 id 以升序模式排序,新数组包含记录的父级和子级它基本上是用来做什么的。
I cannot figure out how to make a parent, child (by an id) that be put into an new array from an array (loaded by a database).
I need it to be like this:
+- Parent - ID: 4
|
+---- Child record
+---- Child record <-- these children have a parent_id of 4
+---- Child record
|
+- Parent - ID: 5
|
+---- Child record
+---- Child record
+---- Child record
+---- Child record <-- these children have a parent_id of 5
+---- Child record
|
+- Parent - ID: 7
|
+---- Child record
+---- Child record <-- these children have a parent_id of 7
+---- Child record
And so on, the record loaded from the database looks like this:
Array
(
[0] => Array
(
[id] => 1
[info] => this is a child, since sub is 1 and parent_id contains a number
[sub] => 1
[parent_id] => 4
)
[1] => Array
(
[id] => 2
[info] => this is a parent, since sub is 0 and parent_id does not contain a number
[sub] => 0
[parent_id] =>
)
[2] => Array
(
[id] => 1
[info] => this is a child, since sub is 1 and parent_id contains a number
[sub] => 1
[parent_id] => 4
)
.... more records
The SQL is ordered in ascending mode by the row id
, the new array containing parents and child of records what it's basically for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是父 ID 是子信息数组的关键的地方
结果数组看起来像这样
this is where the parent id is the key for the array of child info
The resulting array would look something like this