如何创建带有父级和子级的多维数组无限深度数组来自单个表

发布于 2025-01-11 13:08:12 字数 6973 浏览 0 评论 0原文

我试图创建具有无限深度的多维数组。 我从数据库中选择数据并检查字段“isArray”是否为 true,这意味着该列是父列,然后我尝试创建一个循环以使其查找其子列 'parent_id' =>; $row->id

我期待这样的输出。

array(
[0]=> array(
    'id' => '29',
    'section' => '',
    'sorting' =>'',
    'title' => 'POWERS OF THE BANKO SENTRAL',
    'pdf_file' => '',
    'content' => '',
    'parent_id' => '0',
    'isArray' => array(
            [0] => array(
                'id' => '30',
                'section' => '001',
                'sorting' => '',
                'title' => 'Examination by the Bangko Sentral',
                'pdf_file' => 'NRBSL CHRISTMAS PARTY RAFFLE WINNERS.pdf',
                'parent_id' => '29',
                'isArray' => 0,
            ),
            [1] => array(
                'id' => '31',
                'section' => '002',
                'sorting' => '',
                'title' => 'Supervisory Enforcement',
                'pdf_file' => 'APL-Form1.pdf'
                'parent_id' => '29',
                'isArray' => 0
            )
        ),
    ),
[1]=> array(
    [0] => array(
        'id' => '32',
        'section' => '',
        'sorting' => '',
        'title' => 'A. Risk Management',
        'pdf_file' => '',
        'content' => '',
        'parent_id' => '0',
        'isArray' => array(
            [0] => array(
                'id' => '33',
                'section' => '911',
                'sorting' => '',
                'title' => 'RISK MANAGEMENT',
                'pdf_file' => '',
                'content' => '',
                'parent_id' => '32',
                'isArray' => array(
                    [0] => array(
                        'id' => '34',
                        'section' => 'ASDF',
                        'sorting' => '',
                        'title' => 'ASDFSDF',
                        'pdf_file' => '',
                        'content' => '',
                        'parent_id' => '33',
                        'isArray' = array()
                        )
                    )
              )
            )
        )
    )
)
    

我从数据库得到的数据是这样的 输入图片这里的描述

我想出了这段代码:

public function findParentsParent($result) {
            global $subs;
            foreach ($result as $row) {
                $isArray = filter_var($row->isArray, FILTER_VALIDATE_BOOLEAN);
                if ($isArray) {
                    $subs[][$row->parent_id] = $row;
                    $where = ['parent_id' => $row->id];
                    $result = $this->my_model->select_where('tbl_policies', $where, 'sorting, section');
                    if(!empty($result))
                        $this->findParentsParent($result);
                    //return array_reverse($subs);
                } else {
                    $subs[] = $row;
                }
            }
            return array_reverse($subs);
        }

但我最终得到了这个数组:

array(6) {
  [0]=>
  object(stdClass)#44 (11) {
    ["id"]=>
    string(2) "30"
    ["section"]=>
    string(3) "001"
    ["sorting"]=>
    string(0) ""
    ["title"]=>
    string(33) "Examination by the Bangko Sentral"
    ["pdf_file"]=>
    string(41) "NRBSL CHRISTMAS PARTY RAFFLE WINNERS1.pdf"
    ["content"]=>
    string(0) ""
    ["parent_id"]=>
    string(2) "29"
    ["isArray"]=>
    string(1) "0"
    ["uploaded_by"]=>
    string(1) "6"
    ["created_at"]=>
    string(19) "2022-03-03 11:46:06"
    ["updated_at"]=>
    string(19) "2022-03-03 11:46:06"
  }
  [1]=>
  object(stdClass)#43 (11) {
    ["id"]=>
    string(2) "31"
    ["section"]=>
    string(3) "002"
    ["sorting"]=>
    NULL
    ["title"]=>
    string(30) "Supervisory Enforcement Policy"
    ["pdf_file"]=>
    string(13) "APL-Form1.pdf"
    ["content"]=>
    string(0) ""
    ["parent_id"]=>
    string(2) "29"
    ["isArray"]=>
    string(1) "0"
    ["uploaded_by"]=>
    string(1) "6"
    ["created_at"]=>
    string(19) "2022-03-03 13:19:27"
    ["updated_at"]=>
    string(19) "2022-03-03 13:19:27"
  }
  [2]=>
  array(1) {
    [0]=>
    object(stdClass)#40 (11) {
      ["id"]=>
      string(2) "29"
      ["section"]=>
      string(0) ""
      ["sorting"]=>
      string(0) ""
      ["title"]=>
      string(28) "POWERS OF THE BANGKO SENTRAL"
      ["pdf_file"]=>
      string(0) ""
      ["content"]=>
      string(0) ""
      ["parent_id"]=>
      string(1) "0"
      ["isArray"]=>
      string(1) "1"
      ["uploaded_by"]=>
      string(1) "6"
      ["created_at"]=>
      string(19) "2022-03-03 11:45:25"
      ["updated_at"]=>
      string(19) "2022-03-03 11:45:25"
    }
  }
  [3]=>
  array(1) {
    [33]=>
    object(stdClass)#42 (11) {
      ["id"]=>
      string(2) "34"
      ["section"]=>
      string(4) "ASDF"
      ["sorting"]=>
      NULL
      ["title"]=>
      string(7) "ASDFSDF"
      ["pdf_file"]=>
      string(0) ""
      ["content"]=>
      string(0) ""
      ["parent_id"]=>
      string(2) "33"
      ["isArray"]=>
      string(1) "1"
      ["uploaded_by"]=>
      string(1) "6"
      ["created_at"]=>
      string(19) "2022-03-04 09:29:37"
      ["updated_at"]=>
      string(19) "2022-03-04 09:29:37"
    }
  }
  [4]=>
  array(1) {
    [32]=>
    object(stdClass)#41 (11) {
      ["id"]=>
      string(2) "33"
      ["section"]=>
      string(3) "911"
      ["sorting"]=>
      NULL
      ["title"]=>
      string(15) "RISK MANAGEMENT"
      ["pdf_file"]=>
      string(0) ""
      ["content"]=>
      string(0) ""
      ["parent_id"]=>
      string(2) "32"
      ["isArray"]=>
      string(1) "1"
      ["uploaded_by"]=>
      string(1) "6"
      ["created_at"]=>
      string(19) "2022-03-04 09:29:18"
      ["updated_at"]=>
      string(19) "2022-03-04 09:29:18"
    }
  }
  [5]=>
  array(1) {
    [0]=>
    object(stdClass)#39 (11) {
      ["id"]=>
      string(2) "32"
      ["section"]=>
      NULL
      ["sorting"]=>
      NULL
      ["title"]=>
      string(18) "A. Risk Management"
      ["pdf_file"]=>
      string(0) ""
      ["content"]=>
      string(0) ""
      ["parent_id"]=>
      string(1) "0"
      ["isArray"]=>
      string(1) "1"
      ["uploaded_by"]=>
      string(1) "6"
      ["created_at"]=>
      string(19) "2022-03-04 09:28:41"
      ["updated_at"]=>
      string(19) "2022-03-04 09:28:41"
    }
  }
}

Im trying to create multi-dimesional array with unlimited depth.
I select the data from the database and check it if the field 'isArray' is true, meaning this column is a parent then I tried to make a loop to make it look for its child 'parent_id' => $row->id.

I'm expecting output like this.

array(
[0]=> array(
    'id' => '29',
    'section' => '',
    'sorting' =>'',
    'title' => 'POWERS OF THE BANKO SENTRAL',
    'pdf_file' => '',
    'content' => '',
    'parent_id' => '0',
    'isArray' => array(
            [0] => array(
                'id' => '30',
                'section' => '001',
                'sorting' => '',
                'title' => 'Examination by the Bangko Sentral',
                'pdf_file' => 'NRBSL CHRISTMAS PARTY RAFFLE WINNERS.pdf',
                'parent_id' => '29',
                'isArray' => 0,
            ),
            [1] => array(
                'id' => '31',
                'section' => '002',
                'sorting' => '',
                'title' => 'Supervisory Enforcement',
                'pdf_file' => 'APL-Form1.pdf'
                'parent_id' => '29',
                'isArray' => 0
            )
        ),
    ),
[1]=> array(
    [0] => array(
        'id' => '32',
        'section' => '',
        'sorting' => '',
        'title' => 'A. Risk Management',
        'pdf_file' => '',
        'content' => '',
        'parent_id' => '0',
        'isArray' => array(
            [0] => array(
                'id' => '33',
                'section' => '911',
                'sorting' => '',
                'title' => 'RISK MANAGEMENT',
                'pdf_file' => '',
                'content' => '',
                'parent_id' => '32',
                'isArray' => array(
                    [0] => array(
                        'id' => '34',
                        'section' => 'ASDF',
                        'sorting' => '',
                        'title' => 'ASDFSDF',
                        'pdf_file' => '',
                        'content' => '',
                        'parent_id' => '33',
                        'isArray' = array()
                        )
                    )
              )
            )
        )
    )
)
    

And the data I get from the database is this
enter image description here

I came up with this code:

public function findParentsParent($result) {
            global $subs;
            foreach ($result as $row) {
                $isArray = filter_var($row->isArray, FILTER_VALIDATE_BOOLEAN);
                if ($isArray) {
                    $subs[][$row->parent_id] = $row;
                    $where = ['parent_id' => $row->id];
                    $result = $this->my_model->select_where('tbl_policies', $where, 'sorting, section');
                    if(!empty($result))
                        $this->findParentsParent($result);
                    //return array_reverse($subs);
                } else {
                    $subs[] = $row;
                }
            }
            return array_reverse($subs);
        }

But I ended up with this array:

array(6) {
  [0]=>
  object(stdClass)#44 (11) {
    ["id"]=>
    string(2) "30"
    ["section"]=>
    string(3) "001"
    ["sorting"]=>
    string(0) ""
    ["title"]=>
    string(33) "Examination by the Bangko Sentral"
    ["pdf_file"]=>
    string(41) "NRBSL CHRISTMAS PARTY RAFFLE WINNERS1.pdf"
    ["content"]=>
    string(0) ""
    ["parent_id"]=>
    string(2) "29"
    ["isArray"]=>
    string(1) "0"
    ["uploaded_by"]=>
    string(1) "6"
    ["created_at"]=>
    string(19) "2022-03-03 11:46:06"
    ["updated_at"]=>
    string(19) "2022-03-03 11:46:06"
  }
  [1]=>
  object(stdClass)#43 (11) {
    ["id"]=>
    string(2) "31"
    ["section"]=>
    string(3) "002"
    ["sorting"]=>
    NULL
    ["title"]=>
    string(30) "Supervisory Enforcement Policy"
    ["pdf_file"]=>
    string(13) "APL-Form1.pdf"
    ["content"]=>
    string(0) ""
    ["parent_id"]=>
    string(2) "29"
    ["isArray"]=>
    string(1) "0"
    ["uploaded_by"]=>
    string(1) "6"
    ["created_at"]=>
    string(19) "2022-03-03 13:19:27"
    ["updated_at"]=>
    string(19) "2022-03-03 13:19:27"
  }
  [2]=>
  array(1) {
    [0]=>
    object(stdClass)#40 (11) {
      ["id"]=>
      string(2) "29"
      ["section"]=>
      string(0) ""
      ["sorting"]=>
      string(0) ""
      ["title"]=>
      string(28) "POWERS OF THE BANGKO SENTRAL"
      ["pdf_file"]=>
      string(0) ""
      ["content"]=>
      string(0) ""
      ["parent_id"]=>
      string(1) "0"
      ["isArray"]=>
      string(1) "1"
      ["uploaded_by"]=>
      string(1) "6"
      ["created_at"]=>
      string(19) "2022-03-03 11:45:25"
      ["updated_at"]=>
      string(19) "2022-03-03 11:45:25"
    }
  }
  [3]=>
  array(1) {
    [33]=>
    object(stdClass)#42 (11) {
      ["id"]=>
      string(2) "34"
      ["section"]=>
      string(4) "ASDF"
      ["sorting"]=>
      NULL
      ["title"]=>
      string(7) "ASDFSDF"
      ["pdf_file"]=>
      string(0) ""
      ["content"]=>
      string(0) ""
      ["parent_id"]=>
      string(2) "33"
      ["isArray"]=>
      string(1) "1"
      ["uploaded_by"]=>
      string(1) "6"
      ["created_at"]=>
      string(19) "2022-03-04 09:29:37"
      ["updated_at"]=>
      string(19) "2022-03-04 09:29:37"
    }
  }
  [4]=>
  array(1) {
    [32]=>
    object(stdClass)#41 (11) {
      ["id"]=>
      string(2) "33"
      ["section"]=>
      string(3) "911"
      ["sorting"]=>
      NULL
      ["title"]=>
      string(15) "RISK MANAGEMENT"
      ["pdf_file"]=>
      string(0) ""
      ["content"]=>
      string(0) ""
      ["parent_id"]=>
      string(2) "32"
      ["isArray"]=>
      string(1) "1"
      ["uploaded_by"]=>
      string(1) "6"
      ["created_at"]=>
      string(19) "2022-03-04 09:29:18"
      ["updated_at"]=>
      string(19) "2022-03-04 09:29:18"
    }
  }
  [5]=>
  array(1) {
    [0]=>
    object(stdClass)#39 (11) {
      ["id"]=>
      string(2) "32"
      ["section"]=>
      NULL
      ["sorting"]=>
      NULL
      ["title"]=>
      string(18) "A. Risk Management"
      ["pdf_file"]=>
      string(0) ""
      ["content"]=>
      string(0) ""
      ["parent_id"]=>
      string(1) "0"
      ["isArray"]=>
      string(1) "1"
      ["uploaded_by"]=>
      string(1) "6"
      ["created_at"]=>
      string(19) "2022-03-04 09:28:41"
      ["updated_at"]=>
      string(19) "2022-03-04 09:28:41"
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

月下伊人醉 2025-01-18 13:08:12

归功于 从数据库生成多维数组的递归函数结果

我用这段代码解决了我的问题

public function buildTree(array $elements, $parentId = 0) {
            $branch = array();
            foreach ($elements as $element) {
                if ($element['parent_id'] == $parentId) {
                    $children = $this->buildTree($elements, $element['id']);
                    $isArray = filter_var($element['isArray'], FILTER_VALIDATE_BOOLEAN);
                    if ($isArray) {
                        $element['isArray'] = $children;
                    }
                    $branch[] = $element;
                }
            }
            return $branch;
        }

Credits to Recursive function to generate multidimensional array from database result

I solved my problem with this code

public function buildTree(array $elements, $parentId = 0) {
            $branch = array();
            foreach ($elements as $element) {
                if ($element['parent_id'] == $parentId) {
                    $children = $this->buildTree($elements, $element['id']);
                    $isArray = filter_var($element['isArray'], FILTER_VALIDATE_BOOLEAN);
                    if ($isArray) {
                        $element['isArray'] = $children;
                    }
                    $branch[] = $element;
                }
            }
            return $branch;
        }

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