欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql和php无限分类数据归类输出实例讲解

程序员文章站 2022-05-03 19:40:38
说明在先:数据整合部分参考了网上博客;但是数据输出是自己写的,使用了递归; 数据归类解释:利用pid(子级)=id(父级);让子级循环的时自己去找父级在哪儿,然后加入其中; 递...

说明在先:数据整合部分参考了网上博客;但是数据输出是自己写的,使用了递归;

数据归类解释:利用pid(子级)=id(父级);让子级循环的时自己去找父级在哪儿,然后加入其中;

递归:里面需要注意,flag(|–)的问题,下一个父级兄弟要少一个flag(|–)

数据表结构:

CREATE TABLE `NewTable` (
`id`  int(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`pid`  int(10) UNSIGNED NOT NULL ,
`name`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' ,
PRIMARY KEY (`id`),
INDEX `pid` (`pid`) USING BTREE 
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=latin1 COLLATE=latin1_swedish_ci
AUTO_INCREMENT=1
ROW_FORMAT=COMPACT;

源数据

INSERT INTO `test1` VALUES (1, 0, 'menu1');
INSERT INTO `test1` VALUES (2, 0, 'menu2');
INSERT INTO `test1` VALUES (3, 0, 'menu3');
INSERT INTO `test1` VALUES (4, 0, 'menu4');
INSERT INTO `test1` VALUES (5, 0, 'menu5');
INSERT INTO `test1` VALUES (6, 0, 'menu6');
INSERT INTO `test1` VALUES (7, 1, 'menu1-1');
INSERT INTO `test1` VALUES (8, 1, 'menu1-2');
INSERT INTO `test1` VALUES (9, 1, 'menu1-3');
INSERT INTO `test1` VALUES (10, 7, 'menu7-1');
INSERT INTO `test1` VALUES (11, 7, 'menu7-2');
INSERT INTO `test1` VALUES (12, 7, 'menu7-3');
INSERT INTO `test1` VALUES (13, 7, 'menu7-4');
INSERT INTO `test1` VALUES (14, 10, 'menu10-1');
INSERT INTO `test1` VALUES (15, 10, 'menu10-2');
INSERT INTO `test1` VALUES (16, 10, 'menu10-3');
INSERT INTO `test1` VALUES (17, 8, 'menu8-1');

php代码:基于TP5框架

select();

        //新数组:自身id当作key,以便下面操作
        $tree = array();
        foreach ($res as $key => $val) {
            $tree[$val['id']] = $val;
            $tree[$val['id']]['children'] = array();
        }

        //将有pid的元素,整合到对应的pid=id(这一步是最重要的)
        foreach ($tree as $key => $val) {
            if ($val['pid'] != 0) {
                $tree[$val['pid']]['children'][] = &$tree[$key];//$val['pid']=$key;等价于:pid=id
            }
        }

        //去除tree中有pid的元素,因为他们已经被归类了;
        foreach ($tree as $key => $val) {
            if ($val['pid']==0) continue; 
            unset($tree[$key]);
        }

        $temp = array();
        $grup = '';
        $flag = '|--';


        $this->recursive($tree,$temp,$grup,$flag);
        var_dump($temp);exit;

    }

    public function recursive($data,&$temp,&$pision,$flag)
    {
        $piT = $pision;
        if(reset($data)['pid']!=0) $pision .= $flag;

        foreach ($data as $key => $val) {
            $temp[] = $pision.$val['name'];
            if (is_array($val['children'])) {
                $this->recursive($val['children'],$temp,$pision,$flag);
            }
        }

        $pision = $piT;
        return false;

    }

}

新数组输出如下:

array(17) {
  [0] => string(5) "menu1"
  [1] => string(10) "|--menu1-1"
  [2] => string(13) "|--|--menu7-1"
  [3] => string(17) "|--|--|--menu10-1"
  [4] => string(17) "|--|--|--menu10-2"
  [5] => string(17) "|--|--|--menu10-3"
  [6] => string(13) "|--|--menu7-2"
  [7] => string(13) "|--|--menu7-3"
  [8] => string(13) "|--|--menu7-4"
  [9] => string(10) "|--menu1-2"
  [10] => string(13) "|--|--menu8-1"
  [11] => string(10) "|--menu1-3"
  [12] => string(5) "menu2"
  [13] => string(5) "menu3"
  [14] => string(5) "menu4"
  [15] => string(5) "menu5"
  [16] => string(5) "menu6"
}