非递归方式处理无限极菜单

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 15:50   2643   0
<?php
header('content-type: text/html; charset=utf-8');
$array=Array(
 Array('tid' => 1,'tname' => "新手指南",'pid' => 0), 
 Array('tid' => 2, 'tname' => "购物常识",'pid' => 0),
 Array( 'tid' => 3,'tname' => "配送与支付", 'pid' => 0),
 Array('tid' => 4, 'tname' => "服务与保障",'pid' => 0),
 Array( 'tid' => 5, 'tname' => "单页管理",'pid' => 0),
 Array('tid' => 6,'tname' =>" 网站新闻",'pid' => 0),
 Array('tid' => 7,'tname' => "网站公告",'pid' => 0),
 Array('tid' => 8, 'tname' => "test",'pid' => 0 ),
 Array('tid' => 10,'tname' => "售后流程",'pid' => 1),
 Array('tid' => 11,'tname' => "购物流程",'pid' => 1),
 Array('tid' => 12,'tname' => "订购方式",'pid' => 1),
 Array('tid' => 13,'tname' => "如何分辨原装电池 ",'pid' => 2),
 Array('tid' => 14, 'tname' => "如何辨别水货手机", 'pid' => 2),
 Array('tid' => 15,'tname' => "如何享受全国联保",'pid' => 2),
 Array('tid' => 16, 'tname' => "货到付款区域", 'pid' => 3),
 Array('tid' => 17,'tname' => "配送支付智能查询",'pid' => 3),
 Array('tid' => 18,'tname' => "支付方式说明",'pid' => 3),
 Array('tid' => 19,'tname' => "关于我们",'pid' => 5 ),
 Array('tid' => 20,'tname' => "商品促销文章",'pid' => 6),
 Array('tid' => 21, 'tname' => "公司新闻", 'pid' => 6),
 Array('tid' => 22,'tname' =>"商品公告",'pid' => 7),
 Array('tid' => 23,'tname' => "销售公告",'pid' => 7),
 Array('tid' => 24, 'tname' => "日常公告", 'pid' => 7)
);
function actionClassData($data){
 $new_data = array();
 foreach($data as $key=>$val){
  if($val['pid']==0){//当pid为0的话是个新的
   $arr = $val;
   unset($data[$key]);
   foreach($data as $key1=>$val1){
    if($val1['pid']==$arr['tid']){
     $arr['list'][] = $val1;
     unset($data[$key1]);
    }
   }
   $new_data[] = $arr;
  }
 }
 return $new_data;
}


print_r(actionClassData($array));




 

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP