mysql和php無限分類數據歸類輸出實例講解

說明在先:數據整合部分參考瞭網上博客;但是數據輸出是自己寫的,使用瞭遞歸;

數據歸類解釋:利用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"
}

You May Also Like