得到n个深类别树后的多维数组

Multidimensional array after getting n deep category tree

我正在尝试制作一个类别树,它接受每个类别的 n 个 sub-categories 计数。我从网上获得了一些代码并为我的 CI 修改了它,但我无法获得我想要的确切结果。

我有非常简单的数据库:

id,parent_id,标题

如果parent_id = 0 则为主要类别,如果不是则为已保存类别的sub-category。

我有这个方法

public function all_categories($parent_id = 0, $categoriesArray = '', $i = 0) {
        if(!is_array($categoriesArray))
            $categoriesArray = array();

        $allCategories = $this->category_model->get_all(['parent_id' => $parent_id]);
        foreach($allCategories as $category) {
            $categoriesArray[$i] = $category;
            $categoriesArray[$i][] = $this->all_categories($category['id'], $categoriesArray);
        }
        return $categoriesArray;
    }

它对类别进行了很好的排序 - 我得到了主猫和每个 children 的每个子子子类别,但我在 one-dimensional 数组中得到了它们:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_id] => 0
            [title] => Shoes
            [description] => This is the shoes main category
            [image] => 
            [created_at] => 2016-02-18 14:22:56
            [updated_at] => 2016-02-18 14:22:56
            [active] => 
        )

    [1] => Array
        (
            [id] => 2
            [parent_id] => 1
            [title] => Sub-shoes
            [description] => This is sub-category of shoes
            [image] => 
            [created_at] => 2016-02-18 14:27:44
            [updated_at] => 2016-02-18 14:27:44
            [active] => 
        )

    [2] => Array
        (
            [id] => 8
            [parent_id] => 2
            [title] => Sub-sub-shoes
            [description] => This is sub-sub-shoes category
            [image] => 
            [created_at] => 2016-02-21 13:32:44
            [updated_at] => 2016-02-21 13:32:44
            [active] => 
        )

    [3] => Array
        (
            [id] => 5
            [parent_id] => 1
            [title] => Sub-shoes 2
            [description] => This is sub-shoes category after deleting the first of it's kind
            [image] => 
            [created_at] => 2016-02-18 14:40:33
            [updated_at] => 2016-02-18 14:40:33
            [active] => 
        )

    [4] => Array
        (
            [id] => 3
            [parent_id] => 0
            [title] => Dresses
            [description] => This is the main dress category
            [image] => 
            [created_at] => 2016-02-18 14:33:41
            [updated_at] => 2016-02-18 14:33:41
            [active] => 
        )

你能告诉我一种方法吗,这样我就可以订购它们,因为每个主要类别都有索引 'subs',这是一个包含所有 children 的数组,依此类推。

此致!

我认为你必须使用引用:

public function all_categories($parent_id = 0, &$categoriesArray = array()) {
    $allCategories = $this->category_model->get_all(['parent_id' => $parent_id]);
    foreach($allCategories as $category) {
        $categoriesArray[$category['id']] = $category;
        $categoriesArray[$category['id']]['childs'] = array();
        $this->all_categories($category['id'], $categoriesArray[$category['id']]['childs']);
    }
}

$myCategoriesArray = array();
$this->all_categories(0, $myCategoriesArray);

NB : 可能有误,递归循环很费脑筋^^

最好的解决方案是使用对象而不是数组来表示递归结构。

您的问题四的一个简单解决方案是使用一个列表,该列表存储所有可能的父节点的引用,并向您的数组添加一个 children 元素。

这样做可能会导致如下结果

$ids = array();
$root = array(
    'id' => 0,
    'children' => array()
);
$ids[0] = &$root;

foreach ($data as $item) {
    $id = $item['id'];
    $parent_id = (int) $item['parent_id'];

    if (isset($ids[$parent_id])) {
        if (!isset($ids[$parent_id]['children'])) {
            $ids[$parent_id]['children'] = array();
        }
        $ids[$parent_id]['children'][] = &$item;
    }

    $ids[$id] = &$item;
    unset($item);
}

在 运行 这段代码之后,$root 将您的列表包含在树状结构中

array(2) {
  ["id"]=>
  int(0)
  ["children"]=>
  array(2) {
    [0]=>
    &array(3) {
      ["id"]=>
      int(1)
      ["parent_id"]=>
      int(0)
      ["children"]=>
      array(2) {
        [0]=>
        &array(3) {
          ["id"]=>
          int(2)
          ["parent_id"]=>
          int(1)
          ["children"]=>
          array(1) {
            [0]=>
            &array(2) {
              ["id"]=>
              int(8)
              ["parent_id"]=>
              int(2)
            }
          }
        }
        [1]=>
        &array(2) {
          ["id"]=>
          int(5)
          ["parent_id"]=>
          int(1)
        }
      }
    }
    [1]=>
    &array(2) {
      ["id"]=>
      int(3)
      ["parent_id"]=>
      int(0)
    }
  }
}