无限级分类传值引用

tree=array;//IDforeach t r e e = a r r a y ( ) ; / / 第 一 步 , 将 分 类 I D 作 为 数 组 密 , 并 创 建 儿 童 单 元 f o r e a c h ( categories as category$tree[$category[id]]=$category;$tree[$category[id]][children]=array;//children.foreach c a t e g o r y ) $ t r e e [ $ c a t e g o r y [ ′ i d ′ ] ] = $ c a t e g o r y ; $ t r e e [ $ c a t e g o r y [ ′ i d ′ ] ] [ ′ c h i l d r e n ′ ] = a r r a y ( ) ; / / 第 二 步 , 利 用 引 用 , 将 每 个 分 类 添 加 到 父 类 c h i l d r e n 数 组 中 , 这 样 一 次 遍 历 即 可 形 成 树 形 结 构 . f o r e a c h ( tree as key=> k e y => item){ if(item ['pid']!= 0){                              $ tree [                                     $ item [ 'pid' ]  ] [ 'children' ] [] =&$ tree [                                   $ key]; //注意:此处必须传引用否则结果不对if($ tree [                                   $ key] ['children'] == null){unset($ tree)[                                   $ key] ['children']); //如果孩子为空,则删除该孩子元素(任选)}}} 第三步,删除无用的非根节点数据foreach( item ['pid']!= 0){                              $ tree [                                     $ item [ 'pid' ]  ] [ 'children' ] [] =&$ tree [                                   $ key]; //注意:此处必须传引用否则结果不对if($ tree [                                   $ key] ['children'] == null){unset($ tree)[                                   $ key] ['children']); //如果孩子为空,则删除该孩子元素(任选)}}} 第三步,删除无用的非根节点数据foreach( tree as key=> k e y => category){ if(category ['pid']!= 0){unset( category ['pid']!= 0){unset( tree[ $ key]); }}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值