PHP递归实现无限分类数组处理

PHP递归实现无限分类数组处理

1,php递归算法实现无限分类

递归算法对无限分类的数组进行处理,分两种情况,
    一种树状展示(数组包含子数组),
    一种非树状展示(子类按照顺序排在父类的下面,不出现数组包含)

注意:
    在查询数据的时候,也可以把查询语句嵌在递归函数中直接输出,不过原理大同小异。

2,代码

1> 树状展示:

/**
* 分支树显示无限分类
* @param $arr 需要处理的数组
* @param $key 相当于数组单元的id
* @param $fkey 相当于数组单元的父类id
* @param $num 从第几层查起,也就是要查层的父类id
* @return $list 返回的数组
*/
public function recursionTree($arr,$key,$fkey,$num)
{ 
    $list = array();
    foreach($arr as $val){ 
        if($val[$fkey] == $num){ 
            $tmp = $this->recursionTree($arr,$key,$fkey,$val[$key]);
            if($tmp){ 
                $val['son'] = $tmp;
            }
            $list[] = $val;
        }
    }
    return $list;
}



注意:为了语法的简练,if($tmp){              可以直接写成$tmp && $val['son'] = $tmp;
                    $val['son'] = $tmp;
                   }

2>非树状展示

/**
* 分支树显示无限分类
* @param $arr 需要处理的数组
* @param $key 相当于数组单元的id
* @param $fkey 相当于数组单元的父类id
* @param $num 从第几层查起,也就是要查层的父类id
* @param $list 引用数组,用于保存变量
* @return $list 返回的数组
*/
public function recursion($arr,$key,$fkey,$num,&$list=array()){ 
    foreach($arr as $key => $val){ 
        if($val[$fkey] == $num){ 
            $list[] = $val;
            $this->recursion($arr,$val[$key],$list);
        }
    }
    return $list;
}

3>递归查询

public function rcsSelClass($fid,&$list=array())
{ 
    $where['c_fid'] = $fid;
    $field = 'c_id,c_fid,c_oid as oid,c_title,c_describe,c_status,c_path,date_format(from_unixtime(c_addtime),\'%Y/%m/%d %H:%i:%s\')c_addtime';
    $order = 'oid desc';
    $arr = $this->ClassObj
    ->where($where)
    ->field($field)
    ->order($order)
    ->select();
    foreach($arr as $val){ 
        $list[] = $val;
        $this->rcsSelClass($val['c_id'],$list);
    }
    return $list;
}

4>测试数据(下面提供无限分类数据表以及插入语句,方便测试)

数据表:


    CREATE TABLE IF NOT EXISTS `class` (
      `id` int(10) unsigned NOT NULL,
      `fid` int(10) unsigned NOT NULL COMMENT '父类id,如果是顶级分类,那么为0',
      `oid` int(10) unsigned NOT NULL COMMENT '排序id',
      `title` varchar(64) NOT NULL,
      `describe` varchar(255) NOT NULL,
      `addtime` int(11) NOT NULL,
      `status` tinyint(1) NOT NULL DEFAULT '1',
      `path` varchar(255) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;


插入语句:(以中国地名做分类)
    INSERT INTO `class` (`id`, `fid`, `oid`, `title`, `describe`, `addtime`, `status`, `path`) VALUES
    (1, 0, 1, '中国', '中国', 1482915905, 1, '0'),
    (2, 1, 2, '黑龙江', '黑龙江', 1482915947, 1, '0,1'),
    (3, 2, 3, '哈尔滨', '哈尔滨', 1482916008, 1, '0,1,2'),
    (4, 3, 4, '南岗区', '南岗区', 1482916026, 1, '0,1,2,3'),
    (5, 4, 5, '学府路', '学府路', 1482916035, 1, '0,1,2,3,4'),
    (6, 5, 6, '学府三道街', '学府三道街', 1482916051, 1, '0,1,2,3,4,5'),
    (7, 6, 7, '哈尔滨理工大学', '哈尔滨理工大学', 1482916064, 1, '0,1,2,3,4,5,6'),
    (8, 5, 8, '学府四道街', '学府四道街', 1482916081, 1, '0,1,2,3,4,5'),
    (9, 8, 9, '黑龙江大学', '黑龙江大学', 1482916132, 1, '0,1,2,3,4,5,8'),
    (10, 9, 10, '中俄学院', '中俄学院', 1482916145, 1, '0,1,2,3,4,5,8,9'),
    (11, 7, 11, '计算机学院', '计算机学院', 1482916168, 1, '0,1,2,3,4,5,6,7'),
    (12, 2, 12, '齐齐哈尔', '齐齐哈尔', 1482916268, 1, '0,1,2'),
    (13, 12, 13, '富拉尔基', '富拉尔基', 1482916281, 1, '0,1,2,12'),
    (14, 2, 14, '佳木斯', '佳木斯', 1482916297, 1, '0,1,2'),
    (15, 14, 15, '桦川县', '桦川县', 1482916309, 1, '0,1,2,14'),
    (16, 2, 16, '伊春', '伊春', 1482916344, 1, '0,1,2'),
    (17, 2, 17, '大庆', '大庆', 1482916353, 1, '0,1,2'),
    (18, 0, 18, '美国', '美国', 1482916380, 1, '0'),
    (19, 18, 19, '洛杉矶', '洛杉矶', 1482916389, 1, '0,18'),
    (20, 18, 20, '纽约', '纽约', 1482916404, 1, '0,18'),
    (21, 18, 21, '华盛顿', '华盛顿', 1482916416, 1, '0,18'),
    (22, 20, 22, '曼哈顿', '曼哈顿', 1482916440, 1, '0,18,20'),
    (23, 0, 23, '俄罗斯', '俄罗斯', 1482916449, 1, '0'),
    (24, 23, 24, '喀山', '喀山', 1482916458, 1, '0,23'),
    (25, 23, 25, '莫斯科', '莫斯科', 1482916466, 1, '0,23'),
    (26, 23, 26, '圣彼得堡', '圣彼得堡', 1482916476, 1, '0,23'),
    (27, 23, 27, '海参崴', '海参崴', 1482916525, 1, '0,23'),
    (28, 1, 28, '广东', '广东', 1482916546, 1, '0,1'),
    (29, 28, 29, '广州', '广州', 1482916555, 1, '0,1,28'),
    (30, 29, 30, '天河', '天河', 1482916563, 1, '0,1,28,29'),
    (31, 29, 31, '越秀', '越秀', 1482916572, 1, '0,1,28,29'),
    (32, 29, 32, '荔湾', '荔湾', 1482916581, 1, '0,1,28,29'),
    (33, 29, 33, '海珠', '海珠', 1482916589, 1, '0,1,28,29'),
    (34, 29, 34, '番禺', '番禺', 1482916598, 1, '0,1,28,29'),
    (35, 29, 35, '白云', '白云', 1482916671, 1, '0,1,28,29'),
    (36, 30, 36, '棠下', '棠下', 1482916681, 1, '0,1,28,29,30'),
    (37, 36, 37, '科韵路', '科韵路', 1482916690, 1, '0,1,28,29,30,36'),
    (38, 29, 38, '花都', '花都', 1482916711, 1, '0,1,28,29'),
    (39, 36, 39, '棠东', '棠东', 1482917297, 1, '0,1,28,29,30,36'),
    (40, 39, 40, '丰乐牌坊', '丰乐牌坊', 1482917320, 1, '0,1,28,29,30,36,39'),
    (41, 28, 41, '深圳', '深圳', 1482917778, 1, '0,1,28'),
    (42, 41, 42, '南山', '南山', 1482917787, 1, '0,1,28,41'),
    (43, 41, 43, '罗湖', '罗湖', 1482917798, 1, '0,1,28,41'),
    (44, 41, 44, '福田', '福田', 1482917807, 1, '0,1,28,41'),
    (45, 41, 45, '龙华', '龙华', 1482917823, 1, '0,1,28,41'),
    (46, 1, 46, '甘肃', '甘肃', 1482917833, 1, '0,1'),
    (47, 46, 47, '兰州', '兰州', 1482917840, 1, '0,1,46'),
    (50, 1, 50, '江苏', '江苏', 1482917934, 1, '0,1'),
    (49, 46, 49, '天水', '天水', 1482917924, 1, '0,1,46'),
    (51, 28, 51, '东莞', '东莞', 1482917948, 1, '0,1,28'),
    (52, 28, 52, '佛山', '佛山', 1482917960, 1, '0,1,28'),
    (53, 52, 53, '顺德', '顺德', 1482918355, 1, '0,1,28,52'),
    (54, 50, 54, '南京', '南京', 1482918418, 1, '0,1,50'),
    (55, 1, 55, '吉林', '吉林', 1482918427, 1, '0,1'),
    (56, 1, 56, '辽宁', '辽宁', 1482918435, 1, '0,1'),
    (57, 1, 57, '河北', '河北', 1482918442, 1, '0,1'),
    (58, 1, 58, '新疆', '新疆', 1482918451, 1, '0,1'),
    (59, 1, 59, '宁夏', '宁夏', 1482918458, 1, '0,1'),
    (60, 1, 60, '陕西', '陕西', 1482918466, 1, '0,1'),
    (61, 1, 61, '浙江', '浙江', 1482918479, 1, '0,1');

你可能感兴趣的:(递归算法,无限分类,树状展示)