【问题标题】:How to build a multidimensional nested array in PHP/CodeIgniter?如何在 PHP/CodeIgniter 中构建多维嵌套数组?
【发布时间】:2017-04-27 03:46:00
【问题描述】:

假设我有一个表,其中包含表示树层次结构的级别记录

id         group       parent_group_id
---------  ----------  ---------------
1          Parent 1    NULL
2          Parent 2    NULL
3          Parent 3    NULL
4          Child 1     1
5          Child 2     2
6          Child 3     2
7          Child 4     6

我需要构建一个递归函数来构建一个多维嵌套数组,以便它从“顶部”开始,首先构建具有 NULL 的 parent_group_id 的行的顶级数组。快进几次迭代,我希望最终得到一个像这样的对象

$result = array(
    [0] => array(
        'id' => 1,
        'group' => 'Parent 1',
        'parent_group_id' => NULL,
        'children' => array(
            [0] => array(
                'id' => 4,
                'group' => 'Child 1'
                'parent_group_id' => 1,
                'children' => NULL)),
    [1] => array(
        'id' => 2,
        'group' => 'Parent 2',
        'parent_group_id' => NULL,
        'children' => array(
            [0] => array(
                'id' => 5,
                'group' => 'Child 2'
                'parent_group_id' => 2,
                'children' => NULL),
            [1] => array(
                'id' => 6,
                'group' => 'Child 3'
                'parent_group_id' => 2,
                'children' => array(
                     [0] => array(
                         'id' => 1,
                         'group' => 'Child 4'
                         'parent_group_id' => 6,
                         'children' => NULL)))

构建这样的东西的最佳方法是什么?我需要确保它遍历每个“分支”。我猜当它获得顶级父母的 id 时,它会继续检查是否存在任何行,其 parent_group_id 等于第一次运行时的每个 id。然后,如果它找到孩子,获取这些孩子的 id,然后再次检查孩子是否存在。以此类推,直到用完要检查的 id。

我不熟悉 foreach 循环来完成这样的事情。

【问题讨论】:

    标签: php mysql arrays codeigniter multidimensional-array


    【解决方案1】:

    看看这个源代码。

    我觉得这个功能和你问的有点相似。

      public function getAreaTree(array $elements, $parentId = null) {
        $branch = array();
    
        foreach ($elements as $element) {
    
            if ($element['parent_id'] == $parentId) {
    
                $children = getAreaTree($elements, $element['id']);
    
                if ($children) {
    
                    $element['children'] = $children;
    
                }
    
                $branch[] = $element;
            }
    
        }
    
        return empty($branch) ? null : $branch;
    }
    

    【讨论】:

    • 好吧,当我将所有元素的 result_array 传递给 getAreaTree() 的初始调用时,我得到的只是前两个顶级元素,没有子/孙编辑:没关系,我修改了你的代码重命名了一些东西并错过了重命名。
    【解决方案2】:

    您好,我曾研究过您正在寻找的相同概念..

    使用此代码。这对我有用。

    function recursion($parent_id = '') {
            $categories = array();
              $this->db->from('category');
              $this->db->where('parent_id', $parent_id);
              $result = $this->db->get()->result_array();
    
              if(count($result) > 0) {
                  foreach ($result as $c) {
                    $child = $this->recursion($c['category_id']);
                    if($child) {
                        $c ['children']= $child;
                    }
                    $categories[] = $c;
                  }
              }
              return $categories;
    
        }
    
    function get_cat() {
                print_r($this->recursion());
        }
    

    这可以提高预加载所有类别的速度,从而跳过对每个新 parent_id 的查询。

    此函数的作用:加载具有给定 parent_id 的所有类别,遍历所有这些类别并递归存储在数组中。

    但问题是,这会迫使您拥有一个干净的类别树结构。

    希望它能帮助您解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 2023-01-26
      • 2014-02-01
      • 2015-09-02
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多