在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
代码
<?php
/** * 把二维表分解成多级数组,做菜单时常用 * (可用于无线分级菜单,无限级留言) * @author Lonely (从以前BLOG转过来的) * |----------------------| * |id |parentId | value| * |----------------------| * parentId 存id的值,表示该项的父项是id为该项parentId的值的那个记录 * 如上表:parentId为父字段,id为子字段 */ class unLimit { /** * 设置子字段 * @param string */ public static $subIdField='subId'; /** * 设置父字段 * @param string */ public static $parentIdField='parentId'; /** * 设子字段值的键值 * @param string */ public static $subField='sub'; /** * 是否重新分配KEY * @param boolean */ public static $reSortKey=false; /** * 处理分级数组并返回 * @param array $array * @return array */ public static function toSub($array){ if(is_array($array)){ $proarr=array(); foreach($array as $row){ $proarr [$row [self::$parentIdField]] = $row; $proarr [$row [self::$subIdField]] [self::$subField] [$row [self::$parentIdField]] = $row; } $proarr=self::search_sub($proarr,0); if(self::$reSortKey){ $proarr=self::re_sort_key($proarr); } return $proarr; }else return $array; } /** * 关键算法函数 * @param array $array * @param string $key * @return array */ private static function search_sub(array $array,$key){ $return = array (); $subs = isset ( $array [$key] [self::$subField] ) ? $array [$key] [self::$subField] : array (); foreach ( $subs as $k => $v ) { $temp=$v; $temp[self::$subField]=self::search_sub ( $array, $k ); $return [$k] = $temp; } return $return; } /** * 重新排序KEY * @param array $array * @return array */ private static function re_sort_key($array){ $array=array_values($array); foreach($array as $k=>$v){ if(is_array($v[self::$subField])&&!empty($v[self::$subField])){ $array[$k][self::$subField]=self::re_sort_key($v[self::$subField]); } } return $array; } } $temp=array ( 0 => array ( 'a' => '0', 'b' => '1', 'c' => 'a1', ), 1 => array ( 'a' => '0', 'b' => '2', 'c' => 'a2', ), 2 => array ( 'a' => '1', 'b' => '3', 'c' => 'a3', ), 3 => array ( 'a' => '1', 'b' => '4', 'c' => 'a4', ), 4 => array ( 'a' => '3', 'b' => '5', 'c' => 'a5', ), 5 => array ( 'a' => '3', 'b' => '6', 'c' => 'a6', ), 6 => array ( 'a' => '5', 'b' => '7', 'c' => 'a7', ), 7 => array ( 'a' => '5', 'b' => '8', 'c' => 'a8', ), 8 => array ( 'a' => '5', 'b' => '9', 'c' => 'a9', ), 9 => array ( 'a' => '6', 'b' => '10', 'c' => 'a10', ), 10 => array ( 'a' => '6', 'b' => '11', 'c' => 'a11', ), 11 => array ( 'a' => '4', 'b' => '12', 'c' => 'a12', ), 12 => array ( 'a' => '4', 'b' => '13', 'c' => 'a13', ), 13 => array ( 'a' => '12', 'b' => '14', 'c' => 'a14', ), 14 => array ( 'a' => '12', 'b' => '15', 'c' => 'a15', ), 15 => array ( 'a' => '12', 'b' => '16', 'c' => 'a16', ), 16 => array ( 'a' => '13', 'b' => '17', 'c' => 'a17', ), 17 => array ( 'a' => '13', 'b' => '18', 'c' => 'a18', ), 18 => array ( 'a' => '2', 'b' => '19', 'c' => 'a19', ), 19 => array ( 'a' => '2', 'b' => '20', 'c' => 'a20', ), 20 => array ( 'a' => '2', 'b' => '21', 'c' => 'a21', ), 21 => array ( 'a' => '19', 'b' => '22', 'c' => 'a22', ), 22 => array ( 'a' => '19', 'b' => '23', 'c' => 'a23', ), 23 => array ( 'a' => '19', ' 全部评论
专题导读
上一篇:php脚本的执行过程(编译与执行相分离)Apache+PHP的并发访问发布时间:2022-07-10下一篇:php---使用callable强制指定回调类型发布时间:2022-07-10热门推荐
热门话题
阅读排行榜
|
请发表评论