dslh32311 2010-05-27 12:11
浏览 40
已采纳

我们如何将单维数组转换为多维Hierarchical?

I have an single array of Hierarchical categories. Index of the array is the category_id like::

[8846] => Array
    (
        [category_id] => 8846
        [title] => Tsting two
        [description] => Tsting two
        [subtype] => categories
        [type] => object
        [level] => 2
        [parent_category] => 8841
        [tags] => new
        [name] => Tsting two
    )

each value has its parent_category value, I have around 500 elements in the array, what is the best way to make it.

Process i followed:

krsort categories array, so that all the child categories are at the beginning, then

function makeHierarchical() {
  foreach($this->categories as $guid => $category) {
    if($category['level'] != 1)
    $this->multilevel_categories[$category['parent_category']][$guid] = $category;
}

}

but this is not working, it does it only for first level.

Can someone point out me the error.

  • 写回答

1条回答 默认 最新

  • doukuanyong1939 2010-05-27 12:18
    关注

    You might be able to use the answer I posted yesterday to a similar question:

    Convert a series of parent-child relationships into a hierarchical tree?

    Essentially it's the same, you just have more fields in your arrays. Basically you need to traverse the array recursively.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata