制作一个数组,生成记录的父项和子项

发布于 2024-11-07 21:24:28 字数 1284 浏览 1 评论 0原文

我无法弄清楚如何将父级、子级(通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花辞树 2024-11-14 21:24:28
foreach($dbArray as $row)
{
    if($row['parent_id'] != "")
    {
        $parentArray[$row['parent_id']][]['child'] = $row['info'];

    }
    else
    {
        $parentArray[$row['id']]['parent'] = $row['info'];
    }

}

这是父 ID 是子信息数组的关键的地方
结果数组看起来像这样

Array(
/*parent id*/
    [0]=>Array(
       [parent] => //whatever info
       [0] => Array(
               [child]=> //whatever child info
                   )

           )
      )      
foreach($dbArray as $row)
{
    if($row['parent_id'] != "")
    {
        $parentArray[$row['parent_id']][]['child'] = $row['info'];

    }
    else
    {
        $parentArray[$row['id']]['parent'] = $row['info'];
    }

}

this is where the parent id is the key for the array of child info
The resulting array would look something like this

Array(
/*parent id*/
    [0]=>Array(
       [parent] => //whatever info
       [0] => Array(
               [child]=> //whatever child info
                   )

           )
      )      
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文