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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器